我一直following this tutorial。我一直非常小心地按照指示完全按照说明操作。我所做的一切都与人的行为完全相同,只是在我的php文件中我提供了我的主机,用户名,密码和数据库。在我的数据库中,我创建了与教程完全相同的表(相同的字段类型名称,所有内容。我运行相同的脚本),我在Eclipse中拥有完全相同的代码。
这是我的代码。它与给出的相同但我的HttpPost链接是我的。
package com.heatscore.pregame;
import java.io.BufferedReader;
import java.io.InputStream;
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 org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AlertsActivity extends Activity
{
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Uses the scores xml layout
setContentView(R.layout.alerts);
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//http post
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://<EXCLUDED FOR QUESTION>/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
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());
}
//parse json data
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear"));
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
不幸的是,当我运行我的应用程序时,LogCat显示错误:
04-01 00:01:18.483: E/log_tag(5218): Error converting result java.lang.NullPointerException
04-01 00:01:18.483: E/log_tag(5218): Error parsing data org.json.JSONException: End of input at character 0 of
我还为清单文件添加了正确的权限。是的,该行在应用程序结束标记之后。
<uses-permission android:name="android.permission.INTERNET"/>
我ping主机以确保它不是网络问题。结果很成功。此外,我在手机上运行应用程序,而不是模拟器。程序不会崩溃,但日志中会出现错误。
因此,如果几乎与示例相同,那么为什么我的工作不正常?有没有其他人有这个问题,你做了什么?有没有办法解决这个问题?
答案 0 :(得分:0)
想出来! “is”变量(InputStream)位于不同的try catch块中,并且在下一个块中确实为null。
以下代码修复了所有内容。注意我摆脱了一个尝试捕获并将代码从它移动到上一个。
希望这有助于某人。
package xxx.xxx.xxx;
import java.io.BufferedReader;
import java.io.InputStream;
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 org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AlertsActivity extends Activity
{
JSONArray jArray;
String result;
InputStream is;
StringBuilder sb;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.alerts);
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//http post
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection " + e.toString());
}
//parse json data
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: " + json_data.getInt("id")+
", name: " + json_data.getString("name")+
", sex: " + json_data.getInt("sex")+
", birthyear: " + json_data.getInt("birthyear"));
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}