【问题标题】:android set data in external databaseandroid在外部数据库中设置数据
【发布时间】:2016-03-04 04:37:19
【问题描述】:

我想用我的 android 应用程序将数据写入外部 mysql 数据库。

此类适用于:

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);

}
}

我的问题是,如果在我的字符串 (fristname, secondname) 中是 & 或 ?或任何特殊字符,条目将无法正确保存。 有任何想法吗? :)

【问题讨论】:

  • 为什么在添加到 URL 之前不对其进行编码?
  • 怎么样?这是我第一次 :)
  • 请看我的回答。

标签: java android mysql


【解决方案1】:

使用 URLEncoder 类。

请试试这个

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);

注意:不要编码完整的url,只编码参数值。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 2015-06-11
相关资源
最近更新 更多