使用json与php mysql连接的android
我已经在android系统上创建了一个登录活动,需要与php和mysql连接,它工作得很好,但现在我需要转换我的代码以获得JSON响应,但我不知道如何更改php和java中的代码以使我的应用程序适用于JSON 如果有人能帮助我,我将不胜感激 这是我的密码 check.php使用json与php mysql连接的android,php,android,mysql,json,Php,Android,Mysql,Json,我已经在android系统上创建了一个登录活动,需要与php和mysql连接,它工作得很好,但现在我需要转换我的代码以获得JSON响应,但我不知道如何更改php和java中的代码以使我的应用程序适用于JSON 如果有人能帮助我,我将不胜感激 这是我的密码 check.php 尝试使用此响应response=httpclient.execute(httppost)从代码中,将其序列化为JSON,如下所示string responseText=EntityUtils.toString(respons
尝试使用此响应
response=httpclient.execute(httppost)
从代码中,将其序列化为JSON,如下所示string responseText=EntityUtils.toString(response.getEntity());
JSONObject json=新的JSONObject(responseText)代码>
这将把响应放入对象json
将if()语句if(response.equalsIgnoreCase(“User Found”)
更改为if(json.has(“User Found”)
您可能还需要添加缺少的导入
希望这有帮助。在php端,您必须使用函数json_encode()对返回的数据进行编码。请检查此处的文档
例如:
header('Content-type: application/json');
echo json_encode(array('response'=>'user_found'));
在响应的Java/Android端,您必须使用JSONObject,如下所示:
// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(response);
然后,当您在JSONObject中拥有数据时,您可以检查文档以根据需要使用它。
package pack.coderzheaven;
import android.app.Activity;
import android.os.Bundle;
public class UserPage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpage);
}
}
header('Content-type: application/json');
echo json_encode(array('response'=>'user_found'));
// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(response);