Android 致命异常:org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)上的主java.lang.NullPointerException
我正在尝试创建一个项目,尝试从web服务器导入数据,然后在listview中显示,但我得到一个错误,如下所示:Android 致命异常:org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)上的主java.lang.NullPointerException,android,json,jsonexception,Android,Json,Jsonexception,我正在尝试创建一个项目,尝试从web服务器导入数据,然后在listview中显示,但我得到一个错误,如下所示: FATAL EXCEPTION: main java.lang.NullPointerException at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112) 这是我的密码 package com.ku.trying; import java.io.BufferedReader; import j
FATAL EXCEPTION: main
java.lang.NullPointerException
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
这是我的密码
package com.ku.trying;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private String jsonResult;
private String url = "http://127.0.0.1/employee_details.php";
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
accessWebService();
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("employee name");
String number = jsonChildNode.optString("employee no");
String outPut = name + "-" + number;
employeeList.add(createEmployee("employees", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "employees" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}
package com.ku.trying;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
私有字符串jsonResult;
专用字符串url=”http://127.0.0.1/employee_details.php";
私有列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(listView)findViewById(R.id.listView1);
accessWebService();
}
//访问web的异步任务
私有类JsonReadTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(参数[0]);
试一试{
HttpResponse response=httpclient.execute(httppost);
jsonResult=inputStreamToString(
response.getEntity().getContent()).toString();
}
捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
私有StringBuilder inputStreamToString(InputStream为){
字符串rLine=“”;
StringBuilder answer=新建StringBuilder();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(is));
试一试{
而((rLine=rd.readLine())!=null){
答:追加(rLine);
}
}
捕获(IOE异常){
//e.printStackTrace();
Toast.makeText(getApplicationContext(),
“错误…”+e.toString(),Toast.LENGTH_LONG).show();
}
返回答案;
}
@凌驾
受保护的void onPostExecute(字符串结果){
ListDrwaer();
}
}//结束异步任务
public void accessWebService(){
JsonReadTask=新建JsonReadTask();
//传递URL字符串数组的值
执行(新字符串[]{url});
}
//为列表视图生成哈希集
公共无效列表{
List employeeList=新建ArrayList();
试一试{
JSONObject jsonResponse=新的JSONObject(jsonResult);
JSONArray jsonMainNode=jsonResponse.optJSONArray(“emp_info”);
for(int i=0;i
如何解决这个问题………您能发布web响应吗?{“emp_info”:[{“员工姓名”:“aroon”,“员工编号”:“55”}}请尝试jsonChildNode.getString(“员工姓名”);我尝试使用try-jsonChildNode.getString(“员工名称”);试试jsonChildNode.getString(“员工姓名”);还有JSONArray jsonMainNode=jsonResponse.getJSONArray(“emp_info”);但还是同样的错误