1

我正在尝试使用一些使用 php 连接到 mysql 的教程从 URL 获取数据。

但是,当我运行代码时,我得到了一个空指针异常。

Database_demo.java

 package com.database_demo;

 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.List;

 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 org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

 import android.app.ListActivity;
 import android.net.ParseException;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.Toast;

 public class Database_demo extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    List<String> r = new ArrayList<String>();

    try{

    //http post
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://uragon-dev.com/synergy/webservices.php?action=displayAll");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }
    catch(Exception e){
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
   }

    //Convert response to string  
    try
    {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));

      sb = new StringBuilder();

      String line = null;

      while ((line = reader.readLine()) != null) 
      {
         sb.append(line + "\n");
      }

      is.close();

      result = sb.toString();
    }
    catch(Exception e)
    {
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
    }
    //END Convert response to string   
    try{
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++)
            {
               json_data = jArray.getJSONObject(i);
               r.add(json_data.getString("users"));
           }
           setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r));
        }
        catch(JSONException e1){
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
        } catch (ParseException e1) {
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    }

}
 }

logcat 中的错误指向这一行。

 try{
            JSONArray jArray = new JSONArray(result);

这是要在 url http://uragon-dev.com/synergy/webservices.php?action=displayAll上获取的数据

{"users":[{"username":"jokerxvier","firstname":"jason","lastname":"javier"},{"username":"migz","firstname":"miguel","lastname":"rivera"}],"success":1}

导致这种情况的问题可能是什么?另外我想知道如何将数据传递给 url 而不仅仅是从中获取数据。Android新手在这里。谢谢!

4

3 回答 3

0

看起来您正在尝试在主线程上进行网络操作。看看这篇文章如何处理这个: https ://stackoverflow.com/a/6343299/2045570

阅读文档

编辑:

您收到 JSON 错误的原因是您遇到了第一个异常:HERE

    try{

    //http post
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://uragon-dev.com/synergy/webservices.php?action=displayAll");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }
    catch(Exception e){
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();

   }

所以在这里:

JSONArray jArray = new JSONArray(result);

result将始终为空。这是您需要解决的第一个问题。您无法从主线程访问网络。

要查看您的错误消息,请将此行 : 添加e.printStackTrace();到您的 catch 块中,它将android.os.NetworkOnMainThreadException在您的 logcat 输出中打印 a 。

于 2013-09-25T03:11:31.440 回答
0

你有几个问题。您尝试读取的 JSON 不是一个对象数组。所以代码应该是

JSONObject result = new JSONObject(result);
JSONArray users = result.getJSONArray("users");

并且,请不要在主线程上打开网络连接。

于 2013-09-25T03:20:04.557 回答
0

你说错误在JSONArray jArray = new JSONArray(result);. 如果是这种情况,您可以尝试检查 的值result吗?也许用于Log在 Logcat 上显示它。

如果错误是NetworkOnMainThread,您将需要检查来自@user2045570 的答案

希望这有帮助,祝你好运

里德

于 2013-09-25T03:17:54.240 回答