我试图通过使用php连接到mysql的一些教程从URL获取数据。
但是,当我运行代码时,我得到一个nullpointerexception。
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);
以下是在网址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新手在这里。谢谢!
答案 0 :(得分:0)
看起来您正尝试在主线程上尝试网络操作。看看这篇关于如何处理这个问题的帖子:https://stackoverflow.com/a/6343299/2045570
阅读docs
编辑:
您收到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
将始终为null。这是您需要解决的第一个问题。您无法从主线程访问网络。
要查看您的错误消息,请在catch块中添加以下行:e.printStackTrace();
,它将在您的logcat输出中打印android.os.NetworkOnMainThreadException
。
答案 1 :(得分:0)
你说错误发生在JSONArray jArray = new JSONArray(result);
。如果是这种情况,您可以尝试检查result
的值吗?也许使用Log
在Logcat上显示它。
如果错误为NetworkOnMainThread
,您需要查看@ user2045570的答案
希望这有帮助,祝你好运
里德
答案 2 :(得分:0)
你有几个问题。您尝试读取的JSON不是一个对象的数组。所以代码应该是
JSONObject result = new JSONObject(result);
JSONArray users = result.getJSONArray("users");
并且,请不要在主线程上打开网络连接。