发送来自Android的一个JSON HTTP POST请求 [英] Sending a JSON HTTP POST request from Android

查看:198
本文介绍了发送来自Android的一个JSON HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我使用下面的code要发送的发送对象为WCF service.This工程确定,但如果我的WCF服务还需要其他参数会发生什么HTTP POST请求?我怎么能发送他们从我的Andr​​oid客户端? 这是code我写至今:

  StringBuilder的SB =新的StringBuilder();

字符串的HTTP =htt​​p://android.schoolportal.gr/Service.svc/SaveValues​​;


HttpURLConnection类的URLConnection = NULL;
尝试 {
    网址URL =新的URL(HTTP);
    的URLConnection =(HttpURLConnection类)url.openConnection();
    urlConnection.setDoOutput(真正的);
    urlConnection.setRequestMethod(POST);
    urlConnection.setUseCaches(假);
    urlConnection.setConnectTimeout(10000);
    urlConnection.setReadTimeout(10000);
    urlConnection.setRequestProperty(内容类型,应用/ JSON);

    urlConnection.setRequestProperty(主机,android.schoolportal.gr);
    urlConnection.connect();

    //创建的JSONObject这里
    JSONObject的jsonParam =新的JSONObject();
    jsonParam.put(ID,25);
    jsonParam.put(说明,实);
    jsonParam.put(使能,真);
    OutputStreamWriter OUT =新OutputStreamWriter(urlConnection.getOutputStream());
    out.write(jsonParam.toString());
    out.close();

    INT型Htt presult = urlConnection.getResponse code();
    如果(Htt的presult == HttpURLConnection.HTTP_OK){
        的BufferedReader BR =新的BufferedReader(新的InputStreamReader(
            urlConnection.getInputStream(),UTF-8));
        串线= NULL;
        而((行= br.readLine())!= NULL){
            sb.append(行+\ N);
        }
        br.close();

        的System.out.println(+ sb.toString());

    }其他{
            的System.out.println(urlConnection.getResponseMessage());
    }
}赶上(MalformedURLException异常E){

         e.printStackTrace();
}
赶上(IOException异常E){

    e.printStackTrace();
    }赶上(JSONException E){
    // TODO自动生成的catch块
    e.printStackTrace();
}最后{
    如果(URLConnection的!= NULL)
    urlConnection.disconnect();
}
 

解决方案

发布使用参数发布: -

  URL网址;
URLConnection的urlConn;
DataOutputStream类打印输出;
的DataInputStream输入;
URL =新的URL(得到codeBase的()的toString()+env.tcgi。);
urlConn = url.openConnection();
urlConn.setDoInput(真正的);
urlConn.setDoOutput(真正的);
urlConn.setUseCaches(假);
urlConn.setRequestProperty(内容类型,应用/ JSON);
urlConn.setRequestProperty(主机,android.schoolportal.gr);
urlConn.connect();
//创建的JSONObject这里
JSONObject的jsonParam =新的JSONObject();
jsonParam.put(ID,25);
jsonParam.put(说明,实);
jsonParam.put(使能,真);
 

您错过的部分是在下面......即,如下:

  //发送POST输出。
打印输出=新DataOutputStream类(urlConn.getOutputStream());
printout.write(URLEn coder.en code(jsonParam.toString(),UTF-8));
printout.flush();
printout.close();
 

的事情剩下你可以做到这一点。

hi guys i'm using the code below to send an http post request which sends an object to a wcf service.This works ok, but what happens if my wcf service needs also other parameters?how can i send them from my android client? this is the code i've written so far:

StringBuilder sb = new StringBuilder();  

String http = "http://android.schoolportal.gr/Service.svc/SaveValues";  


HttpURLConnection urlConnection=null;  
try {  
    URL url = new URL(http);  
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);   
    urlConnection.setRequestMethod("POST");  
    urlConnection.setUseCaches(false);  
    urlConnection.setConnectTimeout(10000);  
    urlConnection.setReadTimeout(10000);  
    urlConnection.setRequestProperty("Content-Type","application/json");   

    urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
    urlConnection.connect();  

    //Create JSONObject here
    JSONObject jsonParam = new JSONObject();
    jsonParam.put("ID", "25");
    jsonParam.put("description", "Real");
    jsonParam.put("enable", "true");
    OutputStreamWriter out = new   OutputStreamWriter(urlConnection.getOutputStream());
    out.write(jsonParam.toString());
    out.close();  

    int HttpResult =urlConnection.getResponseCode();  
    if(HttpResult ==HttpURLConnection.HTTP_OK){  
        BufferedReader br = new BufferedReader(new InputStreamReader(  
            urlConnection.getInputStream(),"utf-8"));  
        String line = null;  
        while ((line = br.readLine()) != null) {  
            sb.append(line + "\n");  
        }  
        br.close();  

        System.out.println(""+sb.toString());  

    }else{  
            System.out.println(urlConnection.getResponseMessage());  
    }  
} catch (MalformedURLException e) {  

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

    e.printStackTrace();  
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}finally{  
    if(urlConnection!=null)  
    urlConnection.disconnect();  
}  

解决方案

Posting parameters Using POST:-

URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream  input;
url = new URL (getCodeBase().toString() + "env.tcgi");
urlConn = url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");   
urlConn.setRequestProperty("Host", "android.schoolportal.gr");
urlConn.connect();  
//Create JSONObject here
JSONObject jsonParam = new JSONObject();
jsonParam.put("ID", "25");
jsonParam.put("description", "Real");
jsonParam.put("enable", "true");

The part which you missed is in the the following... i.e., as follows..

// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream ());
printout.write(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
printout.flush ();
printout.close ();

The rest of the thing you can do it.

这篇关于发送来自Android的一个JSON HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆