php服务器使用JSON解析简单字符串到Android客户端
大家好,我对Android客户端php服务器使用JSON解析简单字符串到Android客户端,php,android,json,Php,Android,Json,大家好,我对Android客户端phpserver很陌生。我通过JSON遵循了一些post和response变量的教程,但是这个响应错误值转换为JSONObject JSONpost是成功的,但是响应是错误的 Android代码: HttpConnectionParams.setConnectionTimeout(httpParameters, 15000); HttpConnectionParams.setSoTimeout(httpParameters, 15000);
php
server很陌生。我通过JSON
遵循了一些post和response变量的教程,但是这个响应错误<无法将java.lang.String类型的代码>值转换为JSONObject
JSON
post是成功的,但是响应是错误的
Android代码:
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost("http://192.168.1.1/databastest/login.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
// Create a JSON object from the request response
JSONObject jsonObject = new JSONObject(result);
//Retrieve the data from the JSON object
resultLoging = jsonObject.getString("ResultArray");
}catch (Exception e){
Log.e("ClientServerDemo", "Error:", e);
exception = e;
}
return true;
}
@Override
protected void onPostExecute(Boolean valid){
//Update the UI
Toast.makeText(mContext, resultLoging, Toast.LENGTH_LONG).show();
if(exception != null){
Log.i("Error",exception.getMessage());
Toast.makeText(mContext, exception.getMessage(), Toast.LENGTH_LONG).show();
}
}
php代码
mysqli_query($con,"INSERT INTO usersacc
(phone, password) VALUES('$pho', '$pass')");
#Build the result array (Assign keys to the values)
$result_data = array(
'ResultArray' => 'success',
);
#Output the JSON data
echo json_encode($result_data);
插入成功,但结果未完成。用此代码替换您的代码可能会起作用。以对象而不是字符串的形式从服务器获取响应
Object result = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(result.toString());
resultLoging = jsonObject.getString("ResultArray");
尝试打印“jsonObject”数据,找出数据格式。尝试打印“jsonObject”数据,是json格式的吗?Log.i(“jsonformabc”,jsonObject.toString());我使用了这个,但没有响应??使用Log.i(“result”,result.toString());并检查响应。仍然为空。问题出在您的服务器上,服务器未返回数据,请在浏览器中尝试服务器脚本,并检查数据是否正在打印。