加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

安卓与php进行交互的问题

发布时间:2022-10-15 15:30:32 所属栏目:PHP教程 来源:转载
导读: 因为公司保密协议,php网址不方便给,见谅。
用Postman查看该php网站是这样的

我的代码是这样的:
public class JSONExchanger extends AsyncTask {
String url ="login.php";
@Override

因为公司保密协议,php网址不方便给,见谅。

用Postman查看该php网站是这样的

图片说明

我的代码是这样的:

public class JSONExchanger extends AsyncTask {

String url ="login.php";

@Override
protected JSONObject doInBackground(JSONObject... jsonObjects) {
    JSONObject jsonObject = jsonObjects[0];
    HttpClient httpClient = new DefaultHttpClient();
    //HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 100000);
    JSONObject response = null;
    HttpPost httpPost = new HttpPost(url);
    try {
        Log.i("jsonObject",jsonObject.toString());
        StringEntity stringEntity = new StringEntity(jsonObject.toString());

jquery ajax与php交互_php交互_php与html怎么实现交互

// httpPost.addHeader("username","password"); httpPost.setEntity(stringEntity); HttpResponse httpResponse = httpClient.execute(httpPost); String Server_Response = org.apache.http.util.EntityUtils.toString(httpResponse.getEntity()); response = new JSONObject(Server_Response); Log.i("Response", Server_Response); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return response;

jquery ajax与php交互_php交互_php与html怎么实现交互

}

}

然后在MainActivity中php交互

try {

JSONObject toSend = new JSONObject();

toSend.put("username","abc");

toSend.put("password","bcd");

        JSONExchanger jsonExchanger = new JSONExchanger();
        jsonExchanger.execute(new JSONObject[]{toSend});
    } catch (JSONException e) {
        e.printStackTrace();
    }
卤煮之前没有用过安卓和PHP进行交互今天现学的,还请各位大神不吝赐教,谢谢大家!!

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!