无法与Android

时间:2017-01-17 08:19:49

标签: android mysql httpurlconnection

我试图将数据插入数据库。为此,我使用HTTPURLConnection。但我没有在conn2.getOutputStream()中获得任何值。

try {
    HashMap < String, String > cc = checkdata(hm);
    Sendmail(cc);

    Gson gson = new Gson();
    String json = gson.toJson(cc);


    url2 = new URL("http://192.168.1.99/mist/app/inserttask.php");
    conn2 = (HttpURLConnection) url2.openConnection();

    conn2.setReadTimeout(10000);
    conn2.setConnectTimeout(15000);
    conn2.setRequestMethod("POST");
    conn2.setDoInput(true);
    conn2.setDoOutput(true);


    Uri.Builder builder3 = new Uri.Builder()
        .appendQueryParameter("tasklist", "hai");
    String query1 = builder3.build().getEncodedQuery();
    Log.e("eeeor3", query1);
    OutputStream os2 = conn2.getOutputStream();
    BufferedWriter writer2 = new BufferedWriter(new OutputStreamWriter(os2, "UTF-8"));
    //Log.e("eeeor1",query1);
    writer2.write(query1);
    Log.e("eeeor", "written");
    writer2.flush();
    writer2.close();
    os2.close();
    conn2.connect();


} catch (Exception e) {
    Toast.makeText(getApplicationContext(), e.getMessage() + " is Mandatory..", Toast.LENGTH_LONG).show();
}

1 个答案:

答案 0 :(得分:1)

也许你在ui线程上运行这段代码。请尝试使用AsyncTask