Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

http post blackberry (null response)

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

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: I'm sure server sends data

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

P.S: I'm doing it in the blackberry 9930 simulator

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

This is a sample code for POST the data;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...