我正在尝试使用我的HttpURLConnection发送JSON对象。我按照教程,我不明白为什么我的代码不起作用。 HttpURLConnection工作,但是当我调试我的程序时,它不包含json。
我在这里调用doInBackground:
public void clickbuttonRecieve(View v) {
MyAsync performBackgroundTask = new MyAsync();
performBackgroundTask.execute();
}
这是AsyncTask函数:
private class MyAsync extends AsyncTask<Void, Void, Void> {
String LOGIN_URL = "http://bomatec.be/webservice2.php";
DataOutputStream wr;
StringBuilder result = new StringBuilder();
URL url;
protected Void doInBackground(Void... tmps) {
HttpURLConnection conn = null;
try {
url = new URL(LOGIN_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true); //moved here
conn.setDoInput(true);
//conn.connect();
//conn.setReadTimeout(10000);
//conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
JSONObject data = new JSONObject();
data.put("user", "12");
data.put("lat", "50.8");
data.put("lng", "4.3");
String json = data.toString();
OutputStream wr = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(wr, "UTF-8");
osw.write(json);
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("MYAPP", e.getMessage(), e);
return null;
}catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
e.printStackTrace();
e.printStackTrace();
Log.e("MYAPP", e.getMessage(), e);
return null;
}
//conn.disconnect();
return null;
}
}
当我调试我的代码时,这些是值:
[IMG] http://i63.tinypic.com/1192iwg.png[/IMG]
logcat:
12-26 17:27:17.556 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
12-26 17:27:17.556 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
12-26 17:27:17.556 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve interface method 18095: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
12-26 17:27:17.566 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-26 17:27:17.566 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
12-26 17:27:17.566 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve interface method 18099: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
12-26 17:27:17.566 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-26 17:27:18.207 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
12-26 17:27:18.207 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve virtual method 17991: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
12-26 17:27:18.207 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007
12-26 17:27:18.217 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
12-26 17:27:18.217 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve virtual method 497: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-26 17:27:18.227 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-26 17:27:18.227 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
12-26 17:27:18.227 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve virtual method 519: Landroid/content/res/TypedArray;.getType (I)I
12-26 17:27:18.227 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-26 17:27:18.387 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
12-26 17:27:18.387 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve direct method 18614: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
12-26 17:27:18.387 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
12-26 17:27:18.387 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
12-26 17:27:18.387 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve direct method 18614: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
12-26 17:27:18.387 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
12-26 17:27:18.797 10192-10194/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: GC_CONCURRENT freed 216K, 12% free 9585K/10887K, paused 16ms+32ms, total 222ms
12-26 17:27:19.358 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
12-26 17:27:19.358 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve virtual method 190: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
12-26 17:27:19.358 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x6e at 0x00c8
12-26 17:27:19.358 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras
12-26 17:27:19.358 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve instance field 16
12-26 17:27:19.358 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x54 at 0x00e7
12-26 17:27:19.748 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven E/dalvikvm: Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzah
12-26 17:27:19.748 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve check-cast 230 (Landroid/os/UserManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
12-26 17:27:19.748 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x1f at 0x000c
12-26 17:27:19.748 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzb
12-26 17:27:19.748 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve check-cast 25 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
12-26 17:27:19.748 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x1f at 0x000e
12-26 17:27:19.878 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzj
12-26 17:27:19.878 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven W/dalvikvm: VFY: unable to resolve virtual method 421: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
12-26 17:27:19.878 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
12-26 17:27:23.872 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/libEGL: loaded /system/lib/egl/libEGL_mali.so
12-26 17:27:23.952 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so
12-26 17:27:23.952 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so
12-26 17:27:25.434 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven D/OpenGLRenderer: Enabling debug mode 0
12-26 17:27:25.464 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/Choreographer: Skipped 172 frames! The application may be doing too much work on its main thread.
12-26 17:27:28.116 10192-10192/com.example.stevengerrits.bomatec_gerrits_steven I/Choreographer: Skipped 81 frames! The application may be doing too much work on its main thread.
12-26 17:27:38.867 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: java.net.ProtocolException: Connection already established
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:666)
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at com.example.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:222)
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at com.example.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:197)
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-26 17:27:38.917 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-26 17:27:38.947 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-26 17:27:38.957 10192-10722/com.example.stevengerrits.bomatec_gerrits_steven W/System.err: at java.lang.Thread.run(Thread.java:856)