似乎无法从我的 Android 应用程序发送 http get 参数以显示在我的 PHP 页面上


Can't seem to get http get parameters sent from my Android app to display on my PHP page

我似乎无法让我的PHP页面显示我在Android中使用http客户端发送的数据。我现在需要的只是用PHP显示它,这似乎是一个挑战,我知道我做错了什么。

任何指导将不胜感激。 我已经尝试了从var_dump($_SERVER)json_decode的所有方法,以在PHP中显示它。甚至可以在PHP页面上显示它吗?

     private class Connection extends AsyncTask{
    @Override
    protected Object doInBackground(Object[] objects){
        try{
            PostData(R.id.fullscreen_content, 3);
           }
                catch(IOException exception){
                    exception.printStackTrace();
                }
                return null;
            }
}            
protected void PostData(Integer Question_ID,Integer ResponseChosen_ID)        {
       URL url = new URL("http://10.0.2.2:443/SwlLogin.php");
        HttpURLConnection conn =(HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet post = new HttpGet(conn.getURL().toString());
        post.setHeader("Content-type","application/json");
        conn.connect();
        Date date = new Date();
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
        SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss",Locale.UK);
        String nowDate = dt.format(date);
        String nowTime = time.format(date);
        String phpDate = nowDate;
        String phpTime = nowTime;

        ArrayList<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("Question ID", Question_ID.toString()));
        params.add(new BasicNameValuePair("Response_Chosen_ID", ResponseChosen_ID.toString()));
        params.add(new BasicNameValuePair("TimestampDate", phpDate));
        params.add(new BasicNameValuePair("time", phpTime));
        JSONArray array = new JSONArray();
        array.put(params);
        post.setHeader("postData", params.toString());
        post.getParams().setParameter("JSON", params);
        HttpParams var = httpClient.getParams();
        var.setParameter("GET",params);
        HttpResponse response = httpClient.execute(post);
        OutputStreamWriter write = new OutputStreamWriter(conn.getOutputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder builder = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null){
            builder.append(line);
        }
        Log.d("Response:", builder.toString());
        builder.toString();
        reader.close();
 public void happy_click(View view) throws IOException {
    try{
        new Connection().execute();
        report_success();
    }
    catch(Exception exception){
        messageBox("Response was not successful","Failed to process response" + exception.getMessage());
    }
}

您不能在 UI 线程上运行此代码,否则将收到 NetworkRequestOnUIThread 异常。 您必须在不同的线程上执行此操作。

尝试通过以下方式使用 AsyncTask

 private class Uploader extends AsyncTask<Void,Void,Void>{
     protected void doInBackground(){
        // do network request here
      }
     private void onPostExecute(){
        // handle UI updates here as is on ui Thread
     }
 }

或者你可以考虑使用我强烈推荐的OkHTTP库。 为此,请从 okHttp 下载 jar。 将其添加到您的 libs 文件夹中,然后您可以像这样进行网络调用

  MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    JSONObject parcel = new JSONObject();
    try{
        parcel.put("email", emailEdit.getText().toString());
        parcel.put("password", passwordEdit.getText().toString());
        parcel.put("device", "Android");
        parcel.put("hash", "1234");;
    }catch (JSONException e ){
        e.printStackTrace();
    }
    RequestBody body = RequestBody.create(JSON, parcel.toString());
    Request request = new Request.Builder()
            .header("Content-Type", "application/json")
            .url("YOURURL")
            .post(body)
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            if (null != e) {e.printStackTrace();}
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (null != response && response.message().equals(Constants.KEY_OK)) {
                JSONObject serverResponse;
                try{
                    serverResponse = new JSONObject(response.body().string());
                    if(serverResponse.getBoolean(Constants.KEY_SUCCESS)){
                        Constants.getInstance().setToken(serverResponse.getString(Constants.KEY_TOKEN));
                       moveToHomePage();
                    }else{
                        showLoginFail();
                    }
                }catch (JSONException e ){
                    e.printStackTrace();
                }
                response.body().close();
            } else {
                showLoginFail();
            }
        }
    });

还要确保你有

 <uses-permission android:name="...permision.INTERNET"> 

在清单文件中