0

i would like to write data in an external mysql database with my android app.

this class works for that:

public class SendingData extends AppCompatActivity {

Intent intent = null;

private class LoadingDataURL extends AsyncTask<String, String, JSONArray> {




    @Override
    protected JSONArray doInBackground(String... params) {
        URL url;
        HttpURLConnection urlConnection = null;
        JSONArray response = new JSONArray();

        try {
            url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();

            String responseString = readStream(urlConnection.getInputStream());

            intent = new Intent(SendingData.this, Overview.class);
            startActivity(intent);



            response = new JSONArray(responseString);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null)
                urlConnection.disconnect();
        }

        return response;
    }

    private String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            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();
    }
}



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

    String firstname = "Max";
    String secondname= "Mustermann";


    LoadingDataURL client = new LoadingDataURL();
    client.execute("https://domain.com/index.php?"+
                    "fristname="+fristname+
                    "&secondname="+secondname);

}
}

My Problem is, that if in my strings (fristname, secondname) is an & or ? or any special characters, the entry will not be save correctly. any ideas? :)

Trombone0904
  • 4,132
  • 8
  • 51
  • 104

1 Answers1

0

Use the URLEncoder class.

Try this please

String fn = URLEncoder.encode(fristname, "utf-8");
String sn = URLEncoder.encode(secondname, "utf-8");

LoadingDataURL client = new LoadingDataURL();
client.execute("https://domain.com/index.php?"+
                "fristname=" + fn + "&secondname=" + sn);

Note: Don't encode the full url, just the parameter values.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • You might wana see this for that http://stackoverflow.com/questions/213506/java-net-urlencoder-encodestring-is-deprecated-what-should-i-use-instead – Rohit5k2 Dec 01 '15 at 10:27