【问题标题】:post method on androidandroid上的post方法
【发布时间】:2013-06-20 01:26:06
【问题描述】:

我正在使用完美运行的 get 方法。我想知道如何编辑以下代码以接受 post 方法??我想对 post 方法使用与下面相同的代码(进行必要的更改)。

我将不胜感激。

提前致谢。

JsonParsingExample

public class JsonParsingExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        JSONObject json=JSONfunctions.getJSONfromURL("http://example.com/app/login.php?username=a&password=a");
        Log.w("json: ", json.toString());

       }
       }

Jsonfunctions.java

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.w("log_tag", "Error converting result "+e.toString());
        }

        try{

            jArray = new JSONObject(result);            
        }catch(JSONException e){
                Log.w("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

【问题讨论】:

    标签: android json http-post


    【解决方案1】:

    试试这个:

    public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
    
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
    
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    } 
    

    【讨论】:

    • 感谢先生调查问题。我应该使用 jsonfunctions.class 吗??
    • 如果您的目标只是发送 HTTP POST 请求,那么不,您不需要 json 类。您可以将上面的代码直接插入到您的 onCreate 方法中。
    • 那么 jsonfunctions.java 在 get 方法中做了什么,或者换句话说,为什么我在 Get Method 中需要它。请帮我清除这个查询,这对我有很大帮助。如果post 方法花费的时间比预期的要长??
    • 请让我知道上面的查询,我接受我提出的问题的答案。谢谢您,先生。我会尝试一下,如有任何疑问,请与您联系。
    • JSONFunction 是一个类。它里面的函数是 JSONObject ,它只接受一个 URL 参数。如果您希望您的 POST 请求存在于另一个类中并且能够将 URL 传递给它,那么您就是这样做的。有时这对更简洁的代码很有好处,但不是必需的。
    猜你喜欢
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 2019-06-28
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多