0

我正在开发一个 android 应用程序,它向 php 文件发出 HTTP 请求,然后给我一个字符串。该代码在 android 2.x 及更低版本中运行良好,但是当我在更高版本的 android 操作系统中运行它时,它会崩溃。我在清单文件中拥有所有必要的权限。

这是我的代码:

 getData data;
 String result;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    data = new getData("http://alexanderpadin.uphero.com/copyDataBase.php");
    result = data.getResult();

    Log.i("Data", result);
  }

还有我的 getData 类:

 package com.application.uprm_map;

 import java.io.InputStream;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.DefaultHttpClient;
 import android.util.Log;

 public class getData {

     private String result;
     private InputStream is;
     private StringBuilder sb;

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

     public getData(String url){
         try{
             HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(url);
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
              }catch(Exception e){
                  Log.e("log_tag", "Error in http connection "+e.toString());
                  }
         try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
             sb = new StringBuilder();
             sb.append(reader.readLine() + "\n");

             String line="0";
             while ((line = reader.readLine()) != null) {
                 sb.append(line + "\n");
             }
             is.close();
             result = sb.toString();
             }catch(Exception e){
                 Log.e("log_tag", "Error converting result "+e.toString());
             }
     }
     public String getResult(){
         return result;
     }
 }  

日志猫:

04-17 01:29:43.593: E/Trace(13235): error opening trace file: No such file or directory (2)
04-17 01:29:44.118: E/log_tag(13235): Error in http connection android.os.NetworkOnMainThreadException
04-17 01:29:44.118: E/log_tag(13235): Error converting result java.lang.NullPointerException: lock == null
04-17 01:29:44.133: E/AndroidRuntime(13235): FATAL EXCEPTION: main
04-17 01:29:44.133: E/AndroidRuntime(13235): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.uprm_map/com.application.uprm_map.MainActivity}: java.lang.NullPointerException: println needs a message
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.os.Looper.loop(Looper.java:137)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.ActivityThread.main(ActivityThread.java:5226)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at java.lang.reflect.Method.invoke(Method.java:511)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at dalvik.system.NativeStart.main(Native Method)
04-17 01:29:44.133: E/AndroidRuntime(13235): Caused by: java.lang.NullPointerException: println needs a message
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.util.Log.println_native(Native Method)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.util.Log.i(Log.java:159)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at com.application.uprm_map.MainActivity.onCreate(MainActivity.java:64)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.Activity.performCreate(Activity.java:5104)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-17 01:29:44.133: E/AndroidRuntime(13235):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
04-17 01:29:44.133: E/AndroidRuntime(13235):    ... 11 more

我也尝试使用这个包: http: //loopj.com/android-async-http/

这是代码:

       String result; 

       protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);

               AsyncHttpClient client = new AsyncHttpClient();
               client.get("http://alexanderpadin.uphero.com/copyDataBase", new AsyncHttpResponseHandler() {
                   @Override
                   public void onSuccess(String response) {
                       result = response;
                   }
               });

               log.i("Data", result);
       }

当我尝试在函数 onSuccess 中打印响应变量时,它可以工作,但是当我尝试让该变量做其他事情时,它就不起作用了。

日志猫:

04-17 01:22:11.358: E/Trace(12475): error opening trace file: No such file or directory (2)
04-17 01:22:26.103: E/Trace(12710): error opening trace file: No such file or directory (2)
04-17 01:22:26.618: E/AndroidRuntime(12710): FATAL EXCEPTION: main
04-17 01:22:26.618: E/AndroidRuntime(12710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.uprm_map/com.application.uprm_map.MainActivity}: java.lang.NullPointerException: println needs a message
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.os.Looper.loop(Looper.java:137)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.ActivityThread.main(ActivityThread.java:5226)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at java.lang.reflect.Method.invoke(Method.java:511)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at dalvik.system.NativeStart.main(Native Method)
04-17 01:22:26.618: E/AndroidRuntime(12710): Caused by: java.lang.NullPointerException: println needs a message
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.util.Log.println_native(Native Method)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.util.Log.i(Log.java:159)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at com.application.uprm_map.MainActivity.onCreate(MainActivity.java:69)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.Activity.performCreate(Activity.java:5104)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-17 01:22:26.618: E/AndroidRuntime(12710):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
04-17 01:22:26.618: E/AndroidRuntime(12710):    ... 11 more

我的代码中是否缺少某些内容?有人可以帮我实现这一目标吗?我将不胜感激任何帮助。谢谢你。

4

1 回答 1

0

你崩溃是因为

 Log.i("Data", result);

正确:

Log.i("Data", ""+result);

不仅仅是检查为什么结果为空。某些操作系统版本无法读取 401 响应消息,需要 hack 才能做到这一点

于 2013-04-17T05:47:54.723 回答