HTTP-POST は初めてです。「 http://otu-git.dyndns.ws/pvm_srv/echo_json.html」というサイトがあります。
{
"USR":
{
"fun":"validarUsuario",
"txtUser":"123",
"md5Passwd":"123"
}
}
そして私が得る応答は
{"success":"true","nombre":"test"}
Android アプリでこれを行う方法を知りたいです。誰かが私を案内できますか?
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost =new HttpPost("http://otu-git.dyndns.ws/pvm_srv/echo_json.html");
HttpResponse response;
StringEntity se;
JSONObject json =new JSONObject();
try {
json.put("fun", "validarUsuario");
json.put("txtUser", "123");
json.put("md5Passwd", "123");
se=new StringEntity(json.toString());
httpPost.setEntity(se);
response=httpClient.execute(httpPost);
Log.i(tag, "JSON "+response.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
これは私のコードです。正しいか確認していただけますか?アプリのインターネット許可を追加しました。
@Ngathet 次のエラーが表示されます
11-05 14:02:38.489: E/AndroidRuntime(8844): FATAL EXCEPTION: main
11-05 14:02:38.489: E/AndroidRuntime(8844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dhiraj.mpos/com.dhiraj.mpos.LoginActivity}: android.os.NetworkOnMainThreadException
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.ActivityThread.access$600(ActivityThread.java:134)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.os.Looper.loop(Looper.java:137)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.ActivityThread.main(ActivityThread.java:4697)
11-05 14:02:38.489: E/AndroidRuntime(8844): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 14:02:38.489: E/AndroidRuntime(8844): at java.lang.reflect.Method.invoke(Method.java:511)
11-05 14:02:38.489: E/AndroidRuntime(8844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
11-05 14:02:38.489: E/AndroidRuntime(8844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
11-05 14:02:38.489: E/AndroidRuntime(8844): at dalvik.system.NativeStart.main(Native Method)
11-05 14:02:38.489: E/AndroidRuntime(8844): Caused by: android.os.NetworkOnMainThreadException
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1119)
11-05 14:02:38.489: E/AndroidRuntime(8844): at java.net.InetAddress.lookupHostByName(InetAddress.java:441)
11-05 14:02:38.489: E/AndroidRuntime(8844): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
11-05 14:02:38.489: E/AndroidRuntime(8844): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-05 14:02:38.489: E/AndroidRuntime(8844): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-05 14:02:38.489: E/AndroidRuntime(8844): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-05 14:02:38.489: E/AndroidRuntime(8844): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-05 14:02:38.489: E/AndroidRuntime(8844): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-05 14:02:38.489: E/AndroidRuntime(8844): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-05 14:02:38.489: E/AndroidRuntime(8844): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-05 14:02:38.489: E/AndroidRuntime(8844): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-05 14:02:38.489: E/AndroidRuntime(8844): at com.dhiraj.mpos.LoginActivity.insert(LoginActivity.java:88)
11-05 14:02:38.489: E/AndroidRuntime(8844): at com.dhiraj.mpos.LoginActivity.onCreate(LoginActivity.java:45)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.Activity.performCreate(Activity.java:4539)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-05 14:02:38.489: E/AndroidRuntime(8844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2013)
`