簡體   English   中英

Httppost不起作用-在Android中獲取HTML響應而不是JSON

[英]Httppost not working - Getting a html response instead of JSON in Android

我正在嘗試使用httppost連接到服務器,但是由於某些錯誤,我無法執行此操作。 這是我的android代碼:

public class SendPostRequest extends AsyncTask<String, Void, String> {

        protected void onPreExecute(){}

        protected String doInBackground(String... arg0) {

            try{

                URL url = new URL("http://abhirathod.byethost4.com/post.php");

                JSONObject postDataParams = new JSONObject();
                postDataParams.put("name", "abc");
                postDataParams.put("email", "abc@gmail.com");
                Log.e("params", postDataParams.toString());

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(15000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);

                OutputStream os = conn.getOutputStream();

                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(getPostDataString(postDataParams));



                int responseCode=conn.getResponseCode();

                if (responseCode == HttpsURLConnection.HTTP_OK) {

                    BufferedReader in=new BufferedReader(
                            new InputStreamReader(
                                    conn.getInputStream()));
                    StringBuffer sb = new StringBuffer("");
                    String line="";

                    while((line = in.readLine()) != null) {

                        sb.append(line);
                        break;
                    }

                    in.close();
                    return sb.toString();

                }
                else {
                    return new String("false : "+responseCode);
                }

            }
            catch(Exception e){
                return new String("Exception: " + e.getMessage());
            }



        }

        @Override
        protected void onPostExecute(String result) {

            Toast.makeText(getApplicationContext(), result,
                    Toast.LENGTH_LONG).show();

        }
    }

    public String getPostDataString(JSONObject params) throws Exception {

        StringBuilder result = new StringBuilder();
        boolean first = true;

        Iterator<String> itr = params.keys();

        while(itr.hasNext()){

            String key= itr.next();
            Object value = params.get(key);

            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(key, "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(value.toString(), "UTF-8"));

        }
        return result.toString();
    }

我一次調用了execute方法。 我得到以下輸出:

https://s23.postimg.org/47dwhsxvf/Screenshot_2016_12_24_11_05_49_com_spellbee_admi.png

請幫忙!

最后的RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 最后的StringRequest stringRequest = new StringRequest(Request.Method.POST,Appconfig.Url,new Response.Listener(){@Override public void onResponse(String response){

            //compile 'com.mcxiaoke.volley:library:1.0.19'
            // <uses-permission android:name="android.permission.INTERNET"/>
            try {
                jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("Bros");


                for(int i=0; i<jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                    String SONG_ID = jsonObject1.getString("SONG_ID");
                    String GroupID = jsonObject1.getString("SONG_NAME");
                    String SONG_PIC = jsonObject1.getString("SONG_PIC");


                }



            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();

        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> parms = new HashMap<>();
          //  parms.put("MessageID",Model.getMESSAGE_ID());

            return parms;
        }
    };
    requestQueue.add(stringRequest);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM