无法使用json将数据保存到url

时间:2016-03-15 07:23:20

标签: java android sql json

我的json代码{" Status":" NO UPDATE" } 我得到输出,因为无法保存数据。 JSONResult我得到了#34; FAILED" - 当我检查log.v(jsonresult," jsonResult") 我无法解决这个问题 请帮帮我。

btn_Save.setOnClickListener(new OnClickListener() 
    {           public void onClick(View v) 
        {try
            {StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);

                String postReceiverUrl = "http://";
                Log.v(TAG, "postURL: " + postReceiverUrl);
                // HttpClient
                @SuppressWarnings("resource")
                HttpClient httpClient = new DefaultHttpClient();
                // post header
                HttpPost httpPost = new HttpPost(postReceiverUrl);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                Log.v(TAG, jsonobject.toString());

                nameValuePairs.add(new BasicNameValuePair("EmployeeId", wepemployeeDeptId.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("visitorname", wepName.getText().toString()));
                Log.v(TAG, "Visitor Name: " + wepName.getText().toString());
                nameValuePairs.add(new BasicNameValuePair("PhoneNo", wepMobile.getText().toString()));
                //      nameValuePairs.add(new BasicNameValuePair("WhomToMeet", wepWhomToMeet.getSelectedItem().toString()));
                nameValuePairs.add(new BasicNameValuePair("Address", wepAddress.getText().toString()));

                nameValuePairs.add(new BasicNameValuePair("EmailID", wepEmail.getText().toString()));
                //      nameValuePairs.add(new BasicNameValuePair("visitType", wepVisitType.getSelectedItem().toString()));

                nameValuePairs.add(new BasicNameValuePair("PurposeOfMeet", wepPurpose.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("DateofVisit", wepVisitDate.getText().toString()));                   
                nameValuePairs.add(new BasicNameValuePair("Intime", wepInTime.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("Outtime", wepOutTime.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("BadgeNo", wepBadgeNumber.getText().toString()));

                //      nameValuePairs.add(new BasicNameValuePair("CreatedBy", wepcreatedby.getText().toString()));

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // execute HTTP post request
                HttpResponse response = httpClient.execute(httpPost);

                String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();

                Log.i("jsonResult",jsonResult);

                JSONObject object = new JSONObject(jsonResult);

                String status = object.getString("Status").trim();

                Toast.makeText(getBaseContext(), "Please wait...",100).show();

                if(status.toString().equals("SUCCESS"))
                {
                    Intent i = new Intent(Entry.this,MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    Toast.makeText(getBaseContext(), "Details Inserted",1000000).show();
                }

                if(status.toString().equals("FAILED"))
                {
                    Toast.makeText(getBaseContext(), "Couldn't save the data...",1000000).show();
                }
            } 
            catch (JSONException e)
            {
                e.printStackTrace();
            } 
            catch (ClientProtocolException e)
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        } 
    }); 

1 个答案:

答案 0 :(得分:0)

    String data = null;
    try {

        data = URLEncoder.encode("EmployeeId", "UTF-8")
                + "=" + URLEncoder.encode(wepemployeeDeptId.getText().toString(), "UTF-8");
        data += "&" + URLEncoder.encode("visitorname", "UTF-8") + "="
                + URLEncoder.encode(wepName.getText().toString()), "UTF-8");

//根据您的要求添加密钥

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }


    BufferedReader reader = null;

    // Send data
    try {

        // Defined URL  where to send data
        URL url = new URL("Your URL");

        // Send POST data request

        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the server response

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            // Append server response in string
            sb.append(line + "\n");
        }


        text = sb.toString();EmployeeId