android studio httpurlconnection发布到mysql-e/response null

zqry0prt  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(318)

我一直在尝试使用springboot和eclipse中的java代码在android应用程序和mysql数据库之间建立连接。我可以获取用户的数据并在应用程序上查看,但当我试图发帖时,却得到了一个空响应。我一直在使用postman查看问题是否出在服务器内部,但它似乎工作正常。以下是代码,感谢您的帮助:

public class MainActivity extends AppCompatActivity {

    Button btnGET;
    Button btnPOST;
    TextView txtJson;
    ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnGET = (Button) findViewById(R.id.btnGET);
        btnPOST= (Button)findViewById(R.id.btnPST);
        txtJson = (TextView)findViewById(R.id.tvJsonItem);

        btnGET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new JsonTaskGET().execute("http://myipaddress/users");
            }
        });

        btnPOST.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new JsonTaskPOST().execute("http://myipaddress/users");
            }
        });
    }

    private class JsonTaskGET extends AsyncTask<String, String, String> {

        protected void onPreExecute() {
            super.onPreExecute();

            pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("Please wait");
            pd.setCancelable(false);
            pd.show();
        }

        protected String doInBackground(String... params) {

            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = "";

                while ((line = reader.readLine()) != null) {
                    buffer.append(line+"\n");
                    Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)

                }

                return buffer.toString();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (pd.isShowing()){
                pd.dismiss();
            }
            txtJson.setText(result);
        }
    }

    private class JsonTaskPOST extends AsyncTask<String, String, String> {
        String server_response;

        protected void onPreExecute() {
            super.onPreExecute();

            pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("Creating User...");
            pd.setCancelable(true);
            pd.show();
        }

        protected String doInBackground(String... params) {

            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.connect();

                DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream ());

                try {
                    JSONObject obj = new JSONObject();
                    obj.put("username" , "value1");
                    obj.put("password" , "value2");
                    obj.put("name" , "value1");
                    obj.put("email" , "value1");

                    outputStream.writeBytes(obj.toString());
                    Log.e("JSON Input", obj.toString());
                    outputStream.flush();
                    outputStream.close();
                } catch (JSONException ex) {
                    ex.printStackTrace();
                }

                int responseCode = urlConnection.getResponseCode();

                if(responseCode == HttpURLConnection.HTTP_OK){
                    server_response = readStream(urlConnection.getInputStream());
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            if (pd.isShowing()){
                pd.dismiss();
            }

            Log.e("Response", "" + server_response);
        }
    }

    public static String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response.toString();
    }
}

日志:

05-15 11:22:03.044 22325-22385/com.example.isccg.jsonmyqlrestful E/JSON Input!!!: {"username":"value1","password":"value2","name":"value1","email":"value1"}
05-15 11:22:03.045 22325-22385/com.example.isccg.jsonmyqlrestful I/System.out: [OkHttp] sendRequest>>
05-15 11:22:03.046 22325-22385/com.example.isccg.jsonmyqlrestful I/System.out: [OkHttp] sendRequest<<
05-15 11:22:03.553 22325-22341/com.example.isccg.jsonmyqlrestful D/Surface: Surface::disconnect(this=0x9b103700,api=1)
05-15 11:22:03.555 22325-22341/com.example.isccg.jsonmyqlrestful D/GraphicBuffer: unregister, handle(0x9945c180) (w:552 h:270 s:560 f:0x1 u:0x000b00)
05-15 11:22:03.556 22325-22341/com.example.isccg.jsonmyqlrestful D/GraphicBuffer: unregister, handle(0x9945c240) (w:552 h:270 s:560 f:0x1 u:0x000b00)
    unregister, handle(0x9945c300) (w:552 h:270 s:560 f:0x1 u:0x000b00)
05-15 11:22:03.556 22325-22341/com.example.isccg.jsonmyqlrestful D/Surface: Surface::disconnect(this=0x9b103700,api=1)
05-15 11:22:03.581 22325-22325/com.example.isccg.jsonmyqlrestful D/WindowClient: Remove from mViews: DecorView@4410e7[], this = android.view.WindowManagerGlobal@8938432
05-15 11:22:03.581 22325-22325/com.example.isccg.jsonmyqlrestful E/Response: null
05-15 11:22:03.583 22325-22325/com.example.isccg.jsonmyqlrestful V/InputMethodManager: onWindowFocus: null softInputMode=32 first=false flags=#81810100

这就是我得到空响应的地方:

@Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        if (pd.isShowing()){
            pd.dismiss();
        }

        Log.e("Response", "" + server_response);
    }
}

这是我得到的服务器端错误:

.RepositoryRestExceptionHandler : No suitable HttpMessageConverter found to read request body into object of type class com.iscc.restservice.entity.User from request with content type of application/x-www-form-urlencoded;charset=UTF-8!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题