1
我正在使用JSON同時執行兩件事首先我要發送(用戶名&密碼)到PHP文件,它從數據庫中爲用戶名和密碼以及PHP文件讀取切換按鈕狀態發送切換狀態使用JSON列表到應用程序 我結合閱讀和發送用戶名和密碼,但它總是說在Logcat中有一個錯誤接收JSON列表時出錯
JSON代碼我使用組合發送用戶和密碼和接收JSON列表根據到該用戶名和密碼
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(fishtank.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
/*Temp
SharedPreferences settings = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
us = settings.getString("storedWifiUser", "");
ps = settings.getString("storedWifiPass", "");
try {
JSONObject json = new JSONObject();
json.put("user", us);
json.put("pass", ps);
postData(json);
} catch (JSONException e) {
e.printStackTrace();} Temp */
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
/* public void postData(JSONObject json) throws JSONException {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("json", json.toString()));
//httppost.setHeader("Content-type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
Log.i("JSON Response : ",json.toString().trim());
if(response != null) {
InputStream is = response.getEntity().getContent();
//input stream is response that can be shown back on android
}
}catch (Exception e) {
e.printStackTrace();
}
} */
//Temp
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Log.i("JSON Response : ",json.toString().trim());
// Log.i("JSON Response : "+json.toString().trim());
// System.out.println("JSON Response : "+json.toString().trim());
//Temp
JSONObject json2= new JSONObject();
SharedPreferences settings = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
us = settings.getString("storedWifiUser", "");
ps = settings.getString("storedWifiPass", "");
try {
json.put("user", us);
json.put("pass", ps);
} catch (JSONException e) {
e.printStackTrace();}
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("json", json2.toString()));
//httppost.setHeader("Content-type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
Log.i("JSON Response : ",json.toString().trim());
if(response != null) {
InputStream is = response.getEntity().getContent();
//input stream is response that can be shown back on android
}
}catch (Exception e) {
e.printStackTrace();
}
//Temp
JSONObject c = json.getJSONObject("status");
String tog1="";
String tog2="";
String tog3="";
if(c.has("fil"))
tog1 = c.getString("fil");
if(c.has("HEA"))
tog2 = c.getString("HEA");
if(c.has("LED"))
tog3 = c.getString("LED");
Log.i("JSON Response : ",json.toString().trim());
if(tog1.equals("ON"))
{ toggle1.setChecked(true);}
else{ toggle1.setChecked(false);}
if(tog2.equals("ON"))
{ toggle2.setChecked(true);}
else{ toggle2.setChecked(false);}
if(tog3.equals("ON"))
{ toggle3.setChecked(true);}
else{toggle3.setChecked(false);}
} catch (JSONException e) {
e.printStackTrace();
}
}
}//End Json
PHP File
<?php ("Content-type:text/css")
$con=mysqli_connect("localhost","test","123","pet_home");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// if(isset($_POST['json'])){
$json=$_POST['json'];
$data2=json_decode($json,TRUE);
$u=$data2->{'user'};
$p=$data2->{'pass'};
$result = mysqli_query($con,"SELECT * FROM users WHERE username='$u' AND password='$p'");
$row_cnt = mysqli_num_rows($result);
//$row_cnt=1;
if($row_cnt>0){
$row = mysqli_fetch_array($result);
$data = array('success'=>true, 'error'=>'', 'status'=>array("fil" => $row['filter_st'], "HEA"=> $row['heat_st'], "LED" =>$row['led_st']));
}else{
$data = array('success'=>false, 'error' => 'No records found');
}
// }else{
// $data = array('success'=>false, 'error' => 'No POST value from Android App');
// }
echo json_encode($data);
mysqli_close($con);
?>
logcat的
12-13 10:54:43.118: E/AndroidRuntime(8940): FATAL EXCEPTION: main
12-13 10:54:43.118: E/AndroidRuntime(8940): java.lang.NullPointerException
12-13 10:54:43.118: E/AndroidRuntime(8940): at com.set.petshome.fishtank$JSONParse.onPostExecute(fishtank.java:334)
12-13 10:54:43.118: E/AndroidRuntime(8940): at com.set.petshome.fishtank$JSONParse.onPostExecute(fishtank.java:1)
12-13 10:54:43.118: E/AndroidRuntime(8940): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-13 10:54:43.118: E/AndroidRuntime(8940): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-13 10:54:43.118: E/AndroidRuntime(8940): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-13 10:54:43.118: E/AndroidRuntime(8940): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 10:54:43.118: E/AndroidRuntime(8940): at android.os.Looper.loop(Looper.java:137)
12-13 10:54:43.118: E/AndroidRuntime(8940): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-13 10:54:43.118: E/AndroidRuntime(8940): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 10:54:43.118: E/AndroidRuntime(8940): at java.lang.reflect.Method.invoke(Method.java:525)
12-13 10:54:43.118: E/AndroidRuntime(8940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-13 10:54:43.118: E/AndroidRuntime(8940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-13 10:54:43.118: E/AndroidRuntime(8940): at dalvik.system.NativeStart.main(Native Method)
首先你應該運行網絡請求,也許在doInBackground解析,以擺脫NetworkOnMainThreadException – Niko
後ur php輸出 –