2012-01-23 129 views
1

我已经使用这个代码国防部从某些URL hereHTTP POST黑莓(空应答)

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对象的反应,任何想法

PS:我敢肯定,服务器将数据发送

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

PS:我做它在黑莓9930模拟器

+0

你没有提到,你想在整个帖子里想要什么?你说我得到像房子一样的“空”这是什么意思? – alishaik786

+0

这将是一个JSON对象的响应。 –

+0

你是否像这样:HttpConnection httpConnection =(HttpConnection)Connector.open(webservice_address +“; interface = wifi”);并检查无线连接;像这样尝试。 – alishaik786

回答

4

尝试这样的示例代码:

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数据的示例代码;

+0

输入流是什么?它没有在代码中定义。 – donparalias

1

驹ü请添加Connector.READ_WRITE在Connector.open

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

你开始了吗? mds? – sivakg2000

+0

我也试过了,但模拟器没有回复好回复 –

+0

我检查了你的代码。它工作正常。我认为你已经提到主机为localhost。所以模拟器无法访问您的网络serveice。所以请把它改为ip。并检查你的URL是在黑莓浏览器中的wokring。 – sivakg2000

0

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