Java開発における文字化けし
文書ディレクトリ Action部 JSPエンド android部 Actionセクション
JSP端子
android部
URLEncoder.encode(params,“utf-8”); トランスコードが必要です.
public class Test extends BaseAction{
String name;
@Action(value="testPost")
public void testPost(){
System.out.println(name);//
response.setCharacterEncoding("utf-8");
try {
String str = new String(name.getBytes("ISO-8859-1"), "UTF-8");
System.out.println(str);//
request.setCharacterEncoding("utf-8");
System.out.println(name);//
response.getWriter().write(" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Action(value="testGet")
public void testGet(){
try {
String str = new String(name.getBytes("ISO-8859-1"), "UTF-8");
System.out.println(str);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JSP端子
android部
URLEncoder.encode(params,“utf-8”); トランスコードが必要です.
public class MainActivity extends Activity {
String name1;
String name2;
String ip;
String responce;
Handler handler = new Handler(){
@Override
public void handleMessage(Message message) {
if (message.what == 0) {
Toast.makeText(getApplicationContext(),message.getData().getString("RES"),Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)findViewById(R.id.textView);
name1 = textView.getText().toString();
EditText edit = findViewById(R.id.editText);
name2 = edit.getText().toString();
EditText editText = findViewById(R.id.editText2);
ip = editText.getText().toString();
Log.d("test1",name1);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
Log.d("test2",name1);
// http://10.0.2.2:8081/NewChat/testPost
responce = sendHttpPost("http://10.0.2.2:8081/NewChat/testPost",name1+name2);
Log.d("test3",name1);
Message message = new Message();
message.what = 0;
Bundle bundle = new Bundle();
bundle.putString("RES",responce);
message.setData(bundle);
handler.sendMessage(message);
}
}.start();
}
});
}
public static String sendHttpPost(String url, String params){
PrintWriter out = null;
BufferedReader in = null;
String result = "-1";
try {
//
String str = "name="+URLEncoder.encode(params,"utf-8");
URL realUrl = new URL(url);
// URL
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
conn.setRequestProperty("Charset", "UTF-8");
conn.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
outputStream.writeBytes(str);
InputStream inputStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line;
while((line=reader.readLine())!=null){
response.append(line);
}
result = response.toString();
}catch (Exception e){
e.printStackTrace();
}
return result;
}
}