运行时发现,当成功时能返回json数据, 一旦失败app端完全没有反映,就是if语句完全不执行,连空的toast框都没有。
检查后发现, 一旦删了下面红色部分的处理json数据的语句, if和else语句都可以正确执行
new Thread(new Runnable() {
public void run()
{
try {
client = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
ArrayList params = new ArrayList();
params.add(new BasicNameValuePair(“id”,id));
HttpPost myget = new HttpPost(“http://192.168.210.51/login2.php”);
myget.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(myget);
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode()!=200){
Looper.prepare();
Toast toast=Toast.makeText(getApplicationContext(),”异常”, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP| Gravity.CENTER, -50, 100);
toast.show();
Looper.loop();
}
String result = EntityUtils.toString(response.getEntity());
//检查php端返回的信息
if(result.equals(“Error”)){
Looper.prepare();
Toast toast=Toast.makeText(getApplicationContext(),result, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP| Gravity.CENTER, -50, 100);
toast.show();
Looper.loop();
}
else{
Looper.prepare();
ArrayList Listfirst = new ArrayList();
ArrayList Listlast = new ArrayList();
ArrayList Listage = new ArrayList();
JSONArray mJSONArray= new JSONArray(result);
for(int i = 0 ; i < mJSONArray.length(); i++)
{
JSONObject jsonItem = mJSONArray.getJSONObject(i);
String firstname = jsonItem.getString(“menu_name”);
String lastname = jsonItem.getString(“url”);
String age = jsonItem.getString(“remark”);
Listfirst.add(firstname);
Listlast.add(lastname);
Listage.add(age);
}
Message msg = new Message();
msg.what = 0x123;
msg.obj =Listfirst.toString();
myHandler.sendMessage(msg);
Toast toast=Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);
toast.show();
Looper.loop();
}
顺便附上php代码,不管有没有用。
<?php
$json = “”;
$data = array();
class User{
public $menu_name;
public $url;
public $remark;
}
$user = new User();
$user->menu_name=”装配单确认”;
$user->url=”http://192.168.208.1/app/android/1_zp.php”;
$user->remark=””;
$data[] = $user;
$user = new User();
$user->menu_name=”装配滞留确认”;
$user->url=”http://192.168.208.1/app/android/1_zl.php”;
$user->remark=””;
$data[] = $user;
$json = json_encode($data);
if(isset($_POST[“id”]))$id = $_POST[“id”];
else $id =””;
if($id ==”C61D4F4″){echo $json;}
else {echo “Error; }
?>
30
70