简体   繁体   中英

android set data in external database

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? :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM