这里的奇怪部分是有效的...然后我尝试了一些不起作用的东西,所以回到我原来的代码,这个代码目前无效。在解析数据期间发生错误。
这是我的代码。
//@SuppressLint("NewApi")
public class MainActivity extends Activity {
InputStream isr = null;
String result = "";
TextView resultView;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
result = new httprRequest().execute().get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getData();
resultView = (TextView) findViewById(R.id.result);
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
*/
public void getData(){
String s = "";
//parse json data
try {
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"Name : "+json.getString("first_name")+"\n"+
"Mobile Using : "+json.getString("mobile_phone")+"\n\n";
}
resultView.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
private class httprRequest extends AsyncTask<String,Integer,String>{
@Override
public String doInBackground(String... params){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("private");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()!=200){
Log.d("MyApp", "Server encontered an error.");
}
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}catch(Exception e){
Log.e("log_entity", "Error in http connection: "+e.toString());
}
//conversion happening here..
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
}
catch(Exception e){
Log.e("log_buf", "Error converting result "+e.toString());
}
return result;
}
}
}
这是我的php文件输出的内容。
[{"first_name":"CHRIS","mobile_phone":"+278285187"},{"first_name":"IAN","mobile_phone":"+64 (27) 582 58"},{"first_name":"ANDRIES","mobile_phone":"+278257922"}]
这是我的PHP代码
connect();
$sql=mysql_query("SELECT first_name, mobile_phone FROM `admin_contacts`");
$output = array();
while($r = mysql_fetch_assoc($sql)){
$output[]=$r;
}
echo json_encode($output);
?>
我的JSOn有效。它抛出一个java.lang.NullPointerException .....如果有帮助的话。
谢谢安德鲁......简直不敢相信我没有看到 - .- ......哈哈,你救了我的话
答案 0 :(得分:0)
这是无效的json,它周围应该有[]
标记为数组
编辑:
看起来实际问题在这里
getData();
resultView = (TextView) findViewById(R.id.result);
getData函数在填充之前使用resultView。交换这些线,这应该解决它