0

我正在处理 POST JSON 数据到服务器。使用此链接的教程可以正常工作,但无法使用某些必需的数据更改。是否有任何与 Javaservlet 和 PHP 链接相关的内容,因为第一个链接是使用 Javasevlet 设计的,而另一个链接是使用 PHP 设计的?

public static String POST(String url)
{
    InputStream inputStream = null;
    String result = "";

    try {
        org.apache.http.client.HttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(url);
        String json = "";

        JSONObject jsonObject=new JSONObject();
        /*JSONArray jsonArray=jsonObject.getJSONArray("object");
        JSONObject jsonObject2=jsonArray.getJSONObject(10);*/
        jsonObject.putOpt("id",2);
        jsonObject.putOpt("address","myaddress");

        json=jsonObject.toString();
        StringEntity se=new StringEntity(json);
        httpPost.setEntity(se);

        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        HttpResponse httpResponse=httpClient.execute(httpPost);

        inputStream = httpResponse.getEntity().getContent();

        // 10. convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (JSONException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
     return result;

}

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

       // person = new PersonLocator();
       // person.setAddresS(myLocationText.getText().toString());

        return POST(urls[0]);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
    }
}

我在这里先向您的帮助表示感谢

4

0 回答 0