비동기 작업을 사용하여 PHP 서버에서 데이터를 가져 오기 위해 HttpPost를 수행하고 있습니다. 기본적으로 PHP 스크립트는 JSON 배열 또는 null을 반환합니다. json 배열 반환 될 때 잘 작동하지만 스크립트가 null을 반환하는 경우 내 if 문이 선택되지 않고이 오류가 반환됩니다.startActivity() from insidePostExecute() not working
데이터를 구문 분석하는 동안 오류가 발생했습니다. org.json.JSONException : type null
@Override
protected Void doInBackground(String... params) {
String url_select = "http://localhost/test.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("id", id));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
if(result == "null"){
this.progressDialog.dismiss();
startActivity(new Intent(viewRandom.this, allDone.class));
}else{
try {
JSONArray Jarray = new JSONArray(result);
for(int i=0;i<Jarray.length();i++){
JSONObject Jasonobject = null;
Jasonobject = Jarray.getJSONObject(i);
String id = Jasonobject.getString("id");
}
this.progressDialog.dismiss();
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
변경과 같은 onPostExecute 뭔가이 경우 (결과 == "널 (null)")을 정수 값을 반환하도록해야하는 경우 (result.equals ("널 (null)")) . – Akram
오류가 발생한 줄을 알려주십시오. – Sunny