Home » Java » java – Sending Data From Android using POST to MySQL database-Exceptionshub

java – Sending Data From Android using POST to MySQL database-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to send data from Android app to the MySQL database with the header encoding type of UTF-8 at the moment my code doesn’t have the ability to encode params to UTF-8, only Latin characters correctly posted and displayed in the application, I want to be able to post and display special letters from French or Lithuanian characters, you can take a look at the method I used to post details into the database using below code:

public void uploadData() {
        Requesthttp request = Requesthttp.create(serverURL);

        request.setMethod("POST")
                .setTimeout(99999999)
                .setLogger(new Logger(Logger.ERROR))
                .addParameter("userid", MainActivity.user_id)
                .addParameter("cid", categoriList.get(category.getSelectedItemPosition()).getCategoryId())
                .addParameter("cityid", cityList.get(city.getSelectedItemPosition()).getCityId())
                .addParameter("purpose", String.valueOf(type.getSelectedItem()))
                .addParameter("name", name.getText().toString())
                .addParameter("description", description.getText().toString())
                .addParameter("bed", bed.getText().toString())
                .addParameter("bath", bath.getText().toString())
                .addParameter("area", area.getText().toString())
                .addParameter("amenities", amenities.getText().toString())
                .addParameter("price", price.getText().toString())
                .addParameter("address", address.getText().toString())
                .addParameter("latitude", destinationLocation.latitude)
                .addParameter("longitude", destinationLocation.longitude);


        if (isimage) {
            request.addParameter("image", new File(imageset.get(0).getPath()));
        }

        if (isfloorplan) {
            request.addParameter("floorplan", new File(floorplanset.get(0).getPath()));
        }

        if (isgallery) {
            request.addParameter("galleryimage[]", galleryset);
        }

        request.setFileUploadListener(new FileUploadListener() {
            @Override
            public void onUploadingFile(File file, long size, long uploaded) {

            }
        })
                .setRequestStateListener(new RequestStateListener() {
                    @Override
                    public void onStart() {
                        dialog.setMessage("Adding...");
                        dialog.setIndeterminate(false);
                        dialog.setCancelable(true);
                        dialog.show();
                    }

                    @Override
                    public void onFinish() {
                        dialog.dismiss();
                    }

                    @Override
                    public void onConnectionError(Exception e) {
                        e.printStackTrace();
                    }
                })
                .setResponseListener(new JsonResponseListener() {
                    @Override
                    public void onOkResponse(JSONObject jsonObject) throws JSONException {
                        JSONArray jsonArray = jsonObject.getJSONArray("200");
                        JSONObject objJson = jsonArray.getJSONObject(0);
                        Constants.successmsg = objJson.getInt("success");
                        message = objJson.getString("msg");
                        if (Constants.successmsg == 0) {

                            Toast.makeText(AddPropertyActivity.this, message, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(AddPropertyActivity.this, message, Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(AddPropertyActivity.this, MainActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();
                        }
                    }

                    @Override
                    public void onErrorResponse(JSONObject jsonObject) {

                    }

                    @Override
                    public void onParseError(JSONException e) {

                    }
                }).execute();
    }

Here is the library I use for posting data:
https://github.com/ander7agar/android-http-client/blob/master/httpclient/src/main/java/com/zanjou/http/request/Request.java

and the code that is responsible for method POST from this library:

switch (method) {
        case GET:
            hrb = new HttpGet();
            List<NameValuePair> nameValuePairList = new ArrayList<>();
            for (Parameter p : getParameters()) {
                if (p instanceof StringParameter) {
                    nameValuePairList.add(new BasicNameValuePair(p.getKey(), (String) p.getValue()));
                }
            }

            if (nameValuePairList.size() > 0) {
                url = url + "?" +
                        URLEncodedUtils.format(nameValuePairList, "utf-8");
            }
            break;
        case POST:
            HttpPost post = new HttpPost();
            post.setEntity(entityWrapper);=
            hrb = post;
            break;
        case PUT:
            HttpPut put = new HttpPut();
            put.setEntity(entityWrapper);
            hrb = put;
            break;
        case DELETE:
            hrb = new HttpDelete();
            break;
        case OPTIONS:
            hrb = new HttpOptions();
            break;
        case TRACE:
            hrb = new HttpTrace();
            break;
        default:
            throw new IllegalArgumentException("Http Method '" + method + "' not supported.");
    }

post.setEntity(new UrlEncodedFormEntity(Parameter,”UTF-8″));
wont work with in post case or at least I dont know how to implement it.

How to&Answers: