android php mysql org.json.JSONException:java.lang.String类型的值
我是android应用程序的新手。。我试着制作这个示例应用程序。。但每次我运行应用程序时,它总是说 错误org.json.JSONException:无法将java.lang.String类型的值br转换为JSONObject 。。在我的日志中。。我希望你能帮我做这件事 我的php文件android php mysql org.json.JSONException:java.lang.String类型的值,android,Android,我是android应用程序的新手。。我试着制作这个示例应用程序。。但每次我运行应用程序时,它总是说 错误org.json.JSONException:无法将java.lang.String类型的值br转换为JSONObject 。。在我的日志中。。我希望你能帮我做这件事 我的php文件 include("connection.php"); $mysqli->query("SET NAMES 'utf8'"); $project_id=$_REQUEST['project_id']; $sq
include("connection.php");
$mysqli->query("SET NAMES 'utf8'");
$project_id=$_REQUEST['project_id'];
$sql="SELECT project_id, project_name FROM project where project_id='$project_id'";
$result=$mysqli->query($sql);
while($e=mysqli_fetch_array($result)){
$flag[project_name]=$e[project_name];
}
print(json_encode($flag));
$mysqli->close();
我的活动
公共类MainActivity扩展了活动{
String project_id;
String project_name;
InputStream is=null;
String result=null;
String line=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e_id=(EditText) findViewById(R.id.editText1);
Button select=(Button) findViewById(R.id.button1);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
project_id=e_id.getText().toString();
select();
}
});
}
public void select(){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("project_id",project_id));
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/select1.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e){
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e){
Log.e("Fail 2", e.toString());
}
Log.i("tagconvertstr", "["+result+"]");
try{
JSONObject json_data = new JSONObject(result);
project_name = (json_data.getString("project_name"));
}
catch(Exception e){
Log.e("Fail 3", e.toString());
}
}
}请发布您的日志。我认为您没有获得JSON格式的输出。可能是服务器错误或其他原因,请检查它。log cat。。is org.json.JSONException:Value在转换为JSONObject之前,您是否记录了php的输出?发布您从服务器获得的json字符串