首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JSON执行HttpPost时没有响应

使用JSON执行HttpPost时没有响应
EN

Stack Overflow用户
提问于 2011-11-10 11:33:07
回答 2查看 5.6K关注 0票数 1

我创建了一个submit按钮,在该按钮中我调用了一个方法"call2“,该方法包含使用JSON执行HttpPost的代码

代码语言:javascript
复制
      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服务代码,它在插入记录后返回“成功”。

代码语言:javascript
复制
     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语句后的日志猫

代码语言:javascript
复制
   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语句没有被执行,我不知道为什么

有人能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-10 11:53:05

正如yorkw所说,我怀疑response.toString()给了你回复文本。这是我通常检查我的回复的方式:

代码语言:javascript
复制
if (response.getStatusLine().getStatusCode() == 200)
{
    HttpEntity entity = response.getEntity();
    json = EntityUtils.toString(entity);
}

显然,如果响应代码不是200,就会有某种类型的错误处理

票数 6
EN

Stack Overflow用户

发布于 2011-11-10 11:42:47

删除if语句

代码语言:javascript
复制
if(response.toString()=="success")

您将在logcat中看到println消息。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8074700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档