android - android上的post方法

标签 android json http-post

我正在使用完美运行的 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;
    }
}

最佳答案

试试这个:

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
}
} 

关于android - android上的post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17203907/

相关文章:

android - 为什么 gradle 说它无法解析 "com.android.support:support-annotations:23.1.1"?

C# MVC4 - 将自定义类列表传回 Javascript

php - html 表单提交调用 php 但不重定向

rest - 使用 WifiESP 库对带有 ESP8266 的 arduino 进行 POST 请求

android - HttpHostConnectException 10.0.2.2 :8080 refused

PHP : Regex, 如何验证 android 和 ios 深层链接 URI

android - 在水平 ScrollView 中单击时如何滚动到芯片?

android - Sharedpreferences 与 Sqlite 数据库

json - 无法访问以JSON中的POST AJAX方式从服务器发送的数据

javascript - 如何使用 nunjucks 和 gulp-data 的 JSON 数据中的 HTML 内容?