【问题标题】:http post blackberry (null response)http post blackberry(空响应)
【发布时间】:2012-01-23 09:46:18
【问题描述】:

我已经从一些 url here 使用了这个代码模块:

    HttpConnection httpConnection = (HttpConnection) Connector.open(webservice_address,Connector.READ_WRITE);
    httpConnection.setRequestMethod(HttpConnection.POST);
    httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
    encPostData.append("category", String.valueOf(category));
    byte[] postData = encPostData.toString().getBytes("UTF-8");

    httpConnection.setRequestProperty("Content-Length", String.valueOf(postData.length));
    OutputStream os = httpConnection.openOutputStream();
    os.write(postData);
    os.flush();
    os.close();
    return httpConnection.getResponseMessage();

但是响应总是一样的,我总是得到一个“null”,当我期望有一个 JSON 对象响应时,你知道吗?

P.S:我确定服务器会发送数据

P.S:我尝试过使用 Connector.READ_WRITE 得到相同的结果。

P.S:我是在黑莓9930模拟器上做的

【问题讨论】:

  • 你不是在整篇文章中提到你想要的吗?你说我得到一个像房子一样的“空”;这是什么意思?
  • 这将是一个带有 JSON 对象的响应。
  • 你是不是这样放的:HttpConnection httpConnection = (HttpConnection) Connector.open(webservice_address+";interface=wifi");并检查wifi连接;试试这样。
  • @alishaik786 我在模拟器里做

标签: blackberry http-post


【解决方案1】:

试试这个示例代码:

public class ConnectionThread extends Thread 
{       
String url;
HttpConnection httpConnection;
InputStream inputStream;
String id="0";  
StringBuffer stringBuffer=new StringBuffer();
public ConnectionThread(String url) 
{   
    this.url=url;       
}
public void run() 
{
    try
    {
        httpConnection=(HttpConnection)Connector.open("Giver Your URL"+";interface=wifi");
        URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);

        oPostData.append("category",id);//Parameters list;  
        oPostData.append("categoryName","Categ1");

        System.out.println("================"+oPostData.toString());
        httpConnection.setRequestMethod(HttpConnection.POST);
        httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, oPostData.getContentType());
        byte [] postBytes = oPostData.getBytes();
        httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
        OutputStream strmOut = httpConnection.openOutputStream();
        strmOut.write(postBytes);
        strmOut.flush();
        strmOut.close();

        int response=httpConnection.getResponseCode();
        if(response==HttpConnection.HTTP_OK)
        {
            inputStream = httpConnection.openInputStream();
            int c;
            while((c=inputStream.read())!=-1)
            {
                stringBuffer.append((char)c);
            }
            callBack(stringBuffer.toString());

        }
        else
        {
            callBack("ERROR");
        }
    }
    catch (Exception e) 
    {
        synchronized (UiApplication.getEventLock()) 
        {
            UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
            StartUp.exceptionHandling(e.getMessage());
        }           
    }
    finally
    {
        try 
        {
            if(httpConnection!=null)
            {
                httpConnection.close();
            }
            if(inputStream!=null)
            {
                inputStream.close();
            }
        } 
        catch (Exception e2) 
        {}          
    }       
}
private void callBack(String response)
{
    if(response.equals("ERROR"))
    {
        UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
        // Put an alert here that "URL Not found";
    }
    else
    {
        try
        {
            System.out.println(response);
            //do what you want;                                     
        }
        catch (Exception e) 
        {
            // Put an alert here that "Data Not found";
        }
    }
}   
}

这是POST数据的示例代码;

【讨论】:

    【解决方案2】:

    请在 Connector.open 中添加 Connector.READ_WRITE

    HttpConnection httpConnection = (HttpConnection) Connector.open(webservice_address,Connector.READ_WRITE);
    

    【讨论】:

    • 我也试过了,但是模拟器反应不好
    • 我已经检查了你的代码。它工作正常。我想你已经提到主机作为本地主机。所以模拟器无法访问您的网络服务。所以请将其更改为 ip。并检查你的网址在黑莓浏览器中是否正常工作。
    【解决方案3】:

    在阅读回复之前,您是否尝试避免致电 os.close()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多