HTTP POST黑莓(空响应) [英] http post blackberry (null response)

查看:144
本文介绍了HTTP POST黑莓(空响应)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用的一些网址这个code MOD:

I have used this code mod from some 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();

但反应总是一样的,我得到一个空任何时候,当我预计将有一个JSON对象的反应,任何想法?

But response is always the same, i obtain a "null all times , when i was expected to have a JSON object response , any idea?

P.S:我敢肯定,服务器将数据发送

P.S: I'm sure server sends data

P.S:我曾尝试与Connector.READ_WRITE具有相同的结果。

P.S: I have tried with Connector.READ_WRITE with the same result.

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

推荐答案

尝试这样的样本code:

Try like this sample code:

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";
        }
    }
}   
}

这是一个示例code为发表中的数据;

This is a sample code for POST the data;

这篇关于HTTP POST黑莓(空响应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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