邮政JSON在android系统 [英] Post JSON in android

查看:138
本文介绍了邮政JSON在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android系统上的HttpClient发布字符串数据
但我厌倦了接收响应状态code 503后 - 服务不可及
返回响应为HTML code为我们的网址。

I want to post String data over HttpClient in android but i'm tired after receive response status code 503 - service unavailable and return response as Html code for our url.

我写在Java应用程序中以下code和我返回数据,但是当我写在Android应用程序相同的code我收到一个异常文件I / O没找到,我疑惑这个案例:

I write in the following Code in JAVA Application and i return the data but when I write the same code in Android Application i receive an exception file I/O not found, I'm Puzzled for this case:

public void goButton(View v)
{

   try{
      URL url = new URL("https://xxxxxxxxx");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        Test ts= new ApiRequest("null","getUserbyID",new String[] {                                         "66868706" });

        String payLoad = ts.toString();    //toSting is override method that create //JSON Object
        System.out.println("--->>> " + payLoad);
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        System.out.println("=================>>> "+ payLoad);


    wr.write(payLoad);
    wr.flush();
   BufferedReader rd = new BufferedReader(new nputStreamReader(conn.getInputStream()));

    String line;
   while ((line = rd.readLine()) != null) {
  System.out.println("-->> " + line);
  response += line;
  }
  wr.close();
   rd.close();
    System.out.println("=================>>> "+ response);


} catch (Exception e) {
    e.printStackTrace();
    System.out.println("=================>>> " + e.toString());
    throw new RuntimeException(e);
}

我试图把在AsynTask,这个线程code,但我收到相同的响应状态code。
我在下面的Andr​​oid code写成的示例数据

I try to put this code in AsynTask, Thread but i receive the same response status code. I write in the following Android code as an example data

public void goButton(View v)
{
 try{
         new Thread(new Runnable() {
     public void run() {

     HttpClient client = new DefaultHttpClient();
     HttpConnectionParams.setConnectionTimeout(client.getParams(),
     10000); // Timeout Limit
     HttpResponse response;

     String url = "https://xxxxxxxxxxxxx";

     JSONObject json = new JSONObject();

     try {

     HttpPost post = new HttpPost(url);
     post.setHeader("Content-type", "application/json");
     json.put("service","null");
     json.put("method", getUserByID.toString());
     json.put("parameters", "1111");
     System.out.println(">>>>>>>>>>>" + json.toString());

     StringEntity se = new StringEntity(json.toString());
     se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
     "application/json"));
     post.setEntity(se);

             String response = client.execute(post);

     if (response != null) {
     String temp = EntityUtils.toString(response.getEntity());

     System.out.println(">>>>>>>>>>>" + temp);
     }
     } catch (Exception e) {
     e.printStackTrace();
     System.out.println(">>>>>>>>>>>" + e.getMessage());

     }
     }
     }).start();
}

请帮我找到这个问题:(

Please Help me to find solution for this problem :(

感谢您提前

推荐答案

嘿穆罕默德·萨利姆

由我提供的code段工程通过以下方式,

The code snippet provided by me works in the following way,

1)的 Android设备的URL +数据发送到服务器

2)服务器[说使用ASP.NET平台的]接收数据,并给出一个确认

2)Server [say ASP.NET platform used] receive the data and gives an acknowledgement

现在应该在客户端(Android版)写的code提供给您,在服务器接收数据的后面部分是

Now the Code which should be written at client side (Android) is provided to you, the later part of receiving that data at server is


  • 服务器需要接收数据

  • 一个WebService应被用来做

  • 实施在服务器端的web服务

  • web服务将被调用每当Android将推动URL +数据

  • 一旦你的数据,操纵它,只要你想

这篇关于邮政JSON在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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