0

我想在 android 中通过 HttpClient 发布字符串数据,但是在收到响应状态代码 503 后我很累 - 服务不可用并将响应作为我们 url 的 Html 代码返回。

我在 JAVA 应用程序中编写了以下代码并返回了数据,但是当我在 Android 应用程序中编写相同的代码时,我收到一个未找到的异常文件 I/O,我对这种情况感到困惑:

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, Thread 但我收到相同的响应状态代码。我写在下面的Android代码中作为示例数据

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();
}

请帮我找到解决这个问题的方法:(

先感谢您

4

2 回答 2

0

这是一个代码片段,希望对您有所帮助。

1)一个带有http get服务的函数

   private String SendDataFromAndroidDevice() {
    String result = "";

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet getMethod = new HttpGet("your url + data appended");

        BufferedReader in = null;
        BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient
                .execute(getMethod);
        in = new BufferedReader(new InputStreamReader(httpResponse
                .getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();
        result = sb.toString(); 


    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

2) 扩展 AsyncTask 的类

   private class HTTPdemo extends
        AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {}

    @Override
    protected String doInBackground(String... params) {
        String result = SendDataFromAndroidDevice();
        return result;
    }

    @Override
    protected void onProgressUpdate(Void... values) {}

    @Override
    protected void onPostExecute(String result) {

        if (result != null && !result.equals("")) {
            try {
                JSONObject resObject = new JSONObject(result);

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

3)在你的 onCreate 方法里面

     @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView("your layout");



    if ("check here where network/internet is avaliable") {

        new HTTPdemo().execute("");
    }

   }

此代码段,Android 设备将通过 URL 向服务器发送数据,现在服务器需要从 URL 获取该数据

于 2013-04-22T07:28:55.507 回答
0

嘿穆罕默德萨利姆

我提供的代码片段按以下方式工作,

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

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

现在向您提供了应该在客户端(Android)编写的代码,在服务器接收该数据的后面部分是

  • 服务器需要接收数据
  • 应该使用网络服务来做到这一点
  • 在服务器端实现 web 服务
  • 每当android推送URL+数据时都会调用webservice
  • 获得数据后,根据需要对其进行操作
于 2013-04-22T07:43:04.377 回答