【问题标题】:No response when doing an HttpPost using JSON使用 JSON 执行 HttpPost 时没有响应
【发布时间】:2011-11-10 03:33:07
【问题描述】:

我创建了一个提交按钮,我在其中调用了一个包含代码的方法“call2” 使用 JSON 进行 HttpPost

      final Button submit = (Button) findViewById(R.id.Button03);
      submit.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v)
          {

            // Perform action on click
             Toast.makeText(display.this,"You have selected to submit data of students",Toast.LENGTH_SHORT).show();
             call2();           
          }

      });      

   } //OnCreate method ends

  After that I have my call2 method as follows:

       public String call2()
        {
      String result="";
      HttpParams httpParams = new BasicHttpParams();
      HttpPost httppost = new HttpPost("http://10.0.2.2/enterdata/Service1.asmx");
          HttpClient client = new DefaultHttpClient(httpParams);

       try
     {


     JSONArray jsArray = new JSONArray(items2);
     jsArray.put(items2);   

     int TIMEOUT_MILLISEC = 10000;  // = 10 seconds

     HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
     HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);


     httppost.setEntity(new ByteArrayEntity(
     result.toString().getBytes("UTF8")));
     HttpResponse response = client.execute(httppost);

     if(response.toString()=="success")
     {
     System.out.println(response);

     }


    }

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

        } 
     return result;

 }

插入记录后返回“成功”的我的 Web 服务代码

     public class Service1 : System.Web.Services.WebService
     {

    [WebMethod]
    public String put(String value)
    {
        int count=1;
        int rows;

        SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
        try
        {
            myConnection.Open();
            count++;
            SqlCommand myCommand = new SqlCommand();
            myCommand.Connection = myConnection;
            myCommand.CommandText = "insert into record values('"+ count +"','" + value + "')";
            myCommand.Parameters.Add("@value", SqlDbType.VarChar).Value = value;
            rows = myCommand.ExecuteNonQuery();
            SqlDataReader myReader = myCommand.ExecuteReader();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            myConnection.Close();
        }

        return "success";
    }

这是我在方法 call2 中删除我的 Android 代码的 If 语句后的日志猫

   11-10 09:19:28.447: INFO/System.out(412):   org.apache.http.message.BasicHttpResponse@44f5d6b8
   11-10 09:22:39.957: WARN/KeyCharacterMap(440): No keyboard for id 0
   11-10 09:22:39.957: WARN/KeyCharacterMap(440): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
   11-10 09:22:40.447: DEBUG/dalvikvm(124): GC_EXPLICIT freed 1224 objects / 67584 bytes in 363ms
   11-10 09:22:42.498: DEBUG/dalvikvm(440): GC_FOR_MALLOC freed 2924 objects / 197768 bytes in 80ms
   11-10 09:22:42.587: INFO/System.out(440): org.apache.http.message.BasicHttpResponse@44f357a0

现在,当我运行我的代码时,我无法在 logcat 中看到任何响应。我的 println 语句没有被执行,我不知道为什么

有人可以帮我吗?

【问题讨论】:

  • 您是否遇到任何异常/超时?您的 println 语句未执行,因为响应不是“成功”
  • 没有看到这个日志猫:pastie.org/2839910

标签: android json web-services http-post


【解决方案1】:

正如 yorkw 所说,我怀疑 response.toString() 是否会为您提供响应文本。这就是我通常检查我的回复的方式:

if (response.getStatusLine().getStatusCode() == 200)
{
    HttpEntity entity = response.getEntity();
    json = EntityUtils.toString(entity);
}

显然,如果响应代码不是 200,则会进行某种错误处理

【讨论】:

  • 检查我发布的 logcat。如何将我得到的响应转换为 String 以查看它到底是什么,因为 toString() 似乎不起作用?
  • 你读过我的代码吗?不是做response.toString(),而是先做response.GetEntity(),然后使用EntityUtils.toString()静态方法从实体获取json响应文本。
  • 是的!我是否需要编写 if 条件,或者在我的情况下是否可以在没有 if 块的情况下编写 2 个语句
  • 我根据你的帖子对我的代码进行了更改,我执行了执行 SOP(json) 的代码...这是我在 logcat 中得到的:pastie.org/2839964
  • 这是一个完全不同的问题,您的 Web 服务设置为 SOAP Web 服务,它不返回 JSON,只返回 XML - 它是 ASP.NET 中的 ASMX 文件吗?看看原始的 ASHX 处理程序来执行 JSON,或者转移到 ASP.NET MVC 或 WCF Web API 之类的东西来执行 JSON。
【解决方案2】:

去掉if语句

if(response.toString()=="success")

您将在 logcat 中看到 println 消息。

顺便说一句,我非常怀疑 response.toString() 是否等于“成功”

【讨论】:

  • 是的,伙计,我删除了 if 语句,我得到了这个:11-10 09:19:28.447: INFO/System.out(412): org.apache.http.message.BasicHttpResponse@44f5d6b8 但是我的网络服务返回成功..我想获得成功..如何做到这一点
  • 关注 mjmarsh 回答或here
猜你喜欢
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多