Android 通过HttpURLConnection发送post数据时发生崩溃和don';我不能正常工作

Android 通过HttpURLConnection发送post数据时发生崩溃和don';我不能正常工作,android,httpurlconnection,asynchttpclient,Android,Httpurlconnection,Asynchttpclient,我想向服务器发送一些json数据 我使用了HttpURLConnection,但我的代码在第行崩溃: PrintWriter out=新的PrintWriter(urlConnection.getOutputStream()) 我想知道我的帖子数据到底有没有发送 public void send(){ String json ="some string"; HttpURLConnection urlConnection = null;

我想向服务器发送一些json数据

我使用了HttpURLConnection,但我的代码在第行崩溃:

PrintWriter out=新的PrintWriter(urlConnection.getOutputStream())

我想知道我的帖子数据到底有没有发送

public void send(){
            String json ="some string";
            HttpURLConnection urlConnection = null;
            try {
                String postdata = "locs=" + URLEncoder.encode(json,"UTF-8");
                URL url = new URL("http://destination/");
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.setFixedLengthStreamingMode(postdata.getBytes().length);
                urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                urlConnection.setConnectTimeout(15000);

                PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
                out.print(postdata);
                out.close();

                String response= "";
                Scanner inStream = new Scanner(urlConnection.getInputStream());
                while(inStream.hasNextLine()){
                    response+=(inStream.nextLine());
                }
                Log.d("states",response);

            } catch (Exception e) {
                Log.d("states", e.getLocalizedMessage());
            }

    }
日志:

编辑: 我把代码改为下面的。但在第二个日志应用程序再次崩溃之前:(

日志:

03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/LocationDetector; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/db/db; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/db/MySQLiteHelper; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteDatabaseConfiguration; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnectionPool; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$OperationLog; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$Operation; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$PreparedStatementCache; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$PreparedStatement; was loaded
03-23 13:28:05.718: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteStatementInfo; was loaded
03-23 13:28:05.718: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteSession; was loaded
03-23 13:28:05.718: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnectionPool$AcquiredConnectionStatus; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/Criteria; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/Criteria$1; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/ILocationListener; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/ILocationListener$Stub; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/LocationManager$ListenerTransport; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/LocationManager$ListenerTransport$1; was loaded
03-23 13:28:08.801: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/ClipboardExManager$1; was loaded
03-23 13:28:08.811: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/IClipboardService; was loaded
03-23 13:28:08.811: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/IClipboardService$Stub; was loaded
03-23 13:28:08.811: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/IClipboardService$Stub$Proxy; was loaded
03-23 13:28:09.472: W/egl_server(4073): Allocating storage 1x1 surface type 0x1
03-23 13:28:09.512: D/xxdalvikvm(4073): XX::Landroid/view/HardwareRenderer$Gl20Renderer$1; was loaded
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/StartMyServiceAtBootReceiver; was loaded
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/senddata; was loaded
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/devices/Devices; was loaded
03-23 13:28:24.436: E/dalvikvm(4073): Could not find class 'com.loopj.android.http.AsyncHttpClient', referenced from method com.example.vatanatomy.senddata.send
03-23 13:28:24.436: W/dalvikvm(4073): VFY: unable to resolve new-instance 890 (Lcom/loopj/android/http/AsyncHttpClient;) in Lcom/example/vatanatomy/senddata;
03-23 13:28:24.436: D/dalvikvm(4073): VFY: replacing opcode 0x22 at 0x0087
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/loctype; was loaded
03-23 13:28:24.436: D/dalvikvm(4073): DexOpt: unable to opt direct call 0x1814 at 0x89 in Lcom/example/vatanatomy/senddata;.send
03-23 13:28:24.436: D/dalvikvm(4073): DexOpt: unable to opt direct call 0x1817 at 0x95 in Lcom/example/vatanatomy/senddata;.send
03-23 13:28:24.436: W/dalvikvm(4073): Unable to resolve superclass of Lcom/example/vatanatomy/senddata$1; (891)
03-23 13:28:24.436: W/dalvikvm(4073): Link of class 'Lcom/example/vatanatomy/senddata$1;' failed
03-23 13:28:24.436: D/dalvikvm(4073): DexOpt: unable to opt direct call 0x180a at 0xa1 in Lcom/example/vatanatomy/senddata;.send
03-23 13:28:24.446: D/states(4073): CONNECTIVITY_CHANGE
03-23 13:28:24.446: D/states(4073): connected to internet
03-23 13:28:24.446: D/xxdalvikvm(4073): XX::Lcom/android/internal/telephony/IPhoneSubInfo$Stub$Proxy; was loaded
03-23 13:28:24.496: D/dalvikvm(4073): GC_CONCURRENT freed 75K, 7% free 9760K/10439K, paused 31ms+2ms, total 47ms
03-23 13:28:24.496: D/dalvikvm(4073): WAIT_FOR_CONCURRENT_GC blocked 16ms
03-23 13:28:24.526: D/dalvikvm(4073): GC_CONCURRENT freed 75K, 7% free 10206K/10887K, paused 2ms+3ms, total 20ms
03-23 13:28:24.526: D/dalvikvm(4073): WAIT_FOR_CONCURRENT_GC blocked 15ms
03-23 13:28:24.556: D/states(4073): 1
03-23 13:28:24.556: D/xxdalvikvm(4073): XX::Ljava/lang/reflect/InvocationTargetException; was loaded
03-23 13:28:24.556: D/AndroidRuntime(4073): Shutting down VM
03-23 13:28:24.556: W/dalvikvm(4073): threadid=1: thread exiting with uncaught exception (group=0x40fd02a0)
03-23 13:28:24.556: D/xxdalvikvm(4073): XX::Ljava/net/UnknownHostException; was loaded
03-23 13:28:24.566: E/AndroidRuntime(4073): FATAL EXCEPTION: main
03-23 13:28:24.566: E/AndroidRuntime(4073): java.lang.NoClassDefFoundError: com.loopj.android.http.AsyncHttpClient
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.example.vatanatomy.senddata.send(senddata.java:103)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.example.vatanatomy.StartMyServiceAtBootReceiver.onReceive(StartMyServiceAtBootReceiver.java:22)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2280)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread.access$1600(ActivityThread.java:140)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.os.Looper.loop(Looper.java:137)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread.main(ActivityThread.java:4921)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at dalvik.system.NativeStart.main(Native Method)

你确定目的链接是真的吗?为什么不像或那样使用库

Loopj的异步HttpClient示例:

String url = "http://www.example.com"
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("key", "value");

client.post(url, params, new AsyncHttpResponseHandler() {
 @Override
 public void onStart() {
     // Initiated the request
 }

 @Override
 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
     // Successfully got a response
 }

 @Override
 public void onFailure(int statusCode, Header[] headers, byte[]     
 responseBody, Throwable
 error)
 {
     // Response failed :(
 }

 @Override
 public void onRetry(int retryNo) {
     // Request was retried
 }

 @Override
 public void onProgress(int bytesWritten, int totalSize) {
     // Progress notification
 }

 @Override
 public void onFinish() {
     // Completed the request (either success or failure)
 });

Log.d(“states”,例如getLocalizedMessage());
在本例中,例如getLocalizedMessage())如果为空,则新的崩溃可能完全不同我在函数中添加了两个日志。当执行函数log1时,但在log2应用程序崩溃之前。新崩溃的stacktrace pls
java.lang.NoClassDefFoundError:com.loopj.android.http.AsyncHttpClient
显然,您使用的两个库在
co>方面存在冲突m、 loopj.AsyncHttpClient
我相信,请添加您的
build.gradle
以查看依赖项我的问题是否已解决。我以错误的方式附加了AsyncHttpClient库!
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/LocationDetector; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/db/db; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/db/MySQLiteHelper; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteDatabaseConfiguration; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnectionPool; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$OperationLog; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$Operation; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$PreparedStatementCache; was loaded
03-23 13:28:05.708: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnection$PreparedStatement; was loaded
03-23 13:28:05.718: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteStatementInfo; was loaded
03-23 13:28:05.718: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteSession; was loaded
03-23 13:28:05.718: D/xxdalvikvm(4073): XX::Landroid/database/sqlite/SQLiteConnectionPool$AcquiredConnectionStatus; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/Criteria; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/Criteria$1; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/ILocationListener; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/ILocationListener$Stub; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/LocationManager$ListenerTransport; was loaded
03-23 13:28:05.728: D/xxdalvikvm(4073): XX::Landroid/location/LocationManager$ListenerTransport$1; was loaded
03-23 13:28:08.801: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/ClipboardExManager$1; was loaded
03-23 13:28:08.811: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/IClipboardService; was loaded
03-23 13:28:08.811: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/IClipboardService$Stub; was loaded
03-23 13:28:08.811: D/xxdalvikvm(4073): XX::Landroid/sec/clipboard/IClipboardService$Stub$Proxy; was loaded
03-23 13:28:09.472: W/egl_server(4073): Allocating storage 1x1 surface type 0x1
03-23 13:28:09.512: D/xxdalvikvm(4073): XX::Landroid/view/HardwareRenderer$Gl20Renderer$1; was loaded
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/StartMyServiceAtBootReceiver; was loaded
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/senddata; was loaded
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/devices/Devices; was loaded
03-23 13:28:24.436: E/dalvikvm(4073): Could not find class 'com.loopj.android.http.AsyncHttpClient', referenced from method com.example.vatanatomy.senddata.send
03-23 13:28:24.436: W/dalvikvm(4073): VFY: unable to resolve new-instance 890 (Lcom/loopj/android/http/AsyncHttpClient;) in Lcom/example/vatanatomy/senddata;
03-23 13:28:24.436: D/dalvikvm(4073): VFY: replacing opcode 0x22 at 0x0087
03-23 13:28:24.436: D/xxdalvikvm(4073): XX::Lcom/example/vatanatomy/loctype; was loaded
03-23 13:28:24.436: D/dalvikvm(4073): DexOpt: unable to opt direct call 0x1814 at 0x89 in Lcom/example/vatanatomy/senddata;.send
03-23 13:28:24.436: D/dalvikvm(4073): DexOpt: unable to opt direct call 0x1817 at 0x95 in Lcom/example/vatanatomy/senddata;.send
03-23 13:28:24.436: W/dalvikvm(4073): Unable to resolve superclass of Lcom/example/vatanatomy/senddata$1; (891)
03-23 13:28:24.436: W/dalvikvm(4073): Link of class 'Lcom/example/vatanatomy/senddata$1;' failed
03-23 13:28:24.436: D/dalvikvm(4073): DexOpt: unable to opt direct call 0x180a at 0xa1 in Lcom/example/vatanatomy/senddata;.send
03-23 13:28:24.446: D/states(4073): CONNECTIVITY_CHANGE
03-23 13:28:24.446: D/states(4073): connected to internet
03-23 13:28:24.446: D/xxdalvikvm(4073): XX::Lcom/android/internal/telephony/IPhoneSubInfo$Stub$Proxy; was loaded
03-23 13:28:24.496: D/dalvikvm(4073): GC_CONCURRENT freed 75K, 7% free 9760K/10439K, paused 31ms+2ms, total 47ms
03-23 13:28:24.496: D/dalvikvm(4073): WAIT_FOR_CONCURRENT_GC blocked 16ms
03-23 13:28:24.526: D/dalvikvm(4073): GC_CONCURRENT freed 75K, 7% free 10206K/10887K, paused 2ms+3ms, total 20ms
03-23 13:28:24.526: D/dalvikvm(4073): WAIT_FOR_CONCURRENT_GC blocked 15ms
03-23 13:28:24.556: D/states(4073): 1
03-23 13:28:24.556: D/xxdalvikvm(4073): XX::Ljava/lang/reflect/InvocationTargetException; was loaded
03-23 13:28:24.556: D/AndroidRuntime(4073): Shutting down VM
03-23 13:28:24.556: W/dalvikvm(4073): threadid=1: thread exiting with uncaught exception (group=0x40fd02a0)
03-23 13:28:24.556: D/xxdalvikvm(4073): XX::Ljava/net/UnknownHostException; was loaded
03-23 13:28:24.566: E/AndroidRuntime(4073): FATAL EXCEPTION: main
03-23 13:28:24.566: E/AndroidRuntime(4073): java.lang.NoClassDefFoundError: com.loopj.android.http.AsyncHttpClient
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.example.vatanatomy.senddata.send(senddata.java:103)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.example.vatanatomy.StartMyServiceAtBootReceiver.onReceive(StartMyServiceAtBootReceiver.java:22)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2280)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread.access$1600(ActivityThread.java:140)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.os.Looper.loop(Looper.java:137)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at android.app.ActivityThread.main(ActivityThread.java:4921)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-23 13:28:24.566: E/AndroidRuntime(4073):     at dalvik.system.NativeStart.main(Native Method)
String url = "http://www.example.com"
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("key", "value");

client.post(url, params, new AsyncHttpResponseHandler() {
 @Override
 public void onStart() {
     // Initiated the request
 }

 @Override
 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
     // Successfully got a response
 }

 @Override
 public void onFailure(int statusCode, Header[] headers, byte[]     
 responseBody, Throwable
 error)
 {
     // Response failed :(
 }

 @Override
 public void onRetry(int retryNo) {
     // Request was retried
 }

 @Override
 public void onProgress(int bytesWritten, int totalSize) {
     // Progress notification
 }

 @Override
 public void onFinish() {
     // Completed the request (either success or failure)
 });