0

在这里我正在收集用户数据,现在我想在提到 url 的情况下发送这些数据。但是当我运行应用程序时,它会显示一个对话框并写为不幸查询已停止。请任何人知道如何发送数据任何 URL 请建议我。

try
    {
        JSONObject action=new JSONObject();

        JSONObject user=new JSONObject();
        action.put("person_name", Person_Name);
        action.put("mobile_number", Mobile_Number);
        action.put("person_query", Person_Query);
        action.put("action", Action);
        user.put("result",action);
       jsonString1 =user.toString();
   }
    catch (Exception je)
    {

    }
     Toast.makeTex(DetailsActivity.this,jsonString1,Toast.LENGTH_LONG).show();


   // Sending JSON Over the Network
     String wurl="http://www.androidbegin.com/tutorial/jsonpar.txt";

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppostreq = new HttpPost(wurl);
    StringEntity se = null;
   try {

    se = new StringEntity( jsonString1);

       } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
       } 
       se.setContentType("application/json;charset=UTF-8");
       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,      "application/json;charset=UTF-8"));
        httppostreq.setEntity(se);
    try {

     HttpResponse httpresponse = httpclient.execute(httppostreq);

         } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
        } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
4

1 回答 1

1

Try this inside your try catch

HttpPost request = new HttpPost(Your URL);
                    JSONObject returnedJObject;
                    returnedJObject = new JSONObject(JSONdata.toString());
                    JSONStringer json = new JSONStringer();
                    StringBuilder sb=new StringBuilder();


                    if (returnedJObject!=null)
                    {
                        Iterator<String> itKeys = returnedJObject.keys();
                        if(itKeys.hasNext())
                            json.object();
                        while (itKeys.hasNext())
                        {
                            String k=itKeys.next();
                            json.key(k).value(returnedJObject.get(k));
                            //Log.e("keys "+k,"value "+returnedJObject.get(k).toString());
                        }             
                    }
                   json.endObject();


                   StringEntity entity;

                   entity = new StringEntity(json.toString());

                   entity.setContentType("application/json;charset=UTF-8");
                  // entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
                   request.setHeader("Accept", "application/json");
                   request.setEntity(entity);

                   HttpResponse response =null;
                   DefaultHttpClient httpClient = new DefaultHttpClient();

                   HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000);
                   HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),50000);

                   response = httpClient.execute(request);

                   InputStream in;
                   in = response.getEntity().getContent();

                   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                   String line = null;
                   while((line = reader.readLine()) != null){
                       sb.append(line);
                   }


                   return new JSONObject(sb.toString());  
于 2013-10-09T07:41:37.357 回答