Blackberry HTTP Post没有回复

时间:2011-12-08 12:49:21

标签: http blackberry post

我试过这段代码,但我没有得到任何结果。

import java.io.*;
import javax.microedition.io.*;
import net.rim.blackberry.api.browser.URLEncodedPostData;
class HttpPost
{
private String _url;
private URLEncodedPostData _postData;
private byte[] _data;
private HttpConnection _httpConnection;
private OutputStream os;
private InputStream is;

HttpPost(String url,URLEncodedPostData data) {
    _postData = postData;
    _url = url;
    try {
      _httpConnection = (HttpConnection)Connector.open(_url);
    } catch(Exception e) {
    }
}

public String postData() {
     try {
        bytes [] postDataBytes = _postData.getBytes();
        _httpConnection.setRequestMethod(HttpConnection.POST);
        _httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
        _httpConnection.setRequestProperty("Content-Language", "en-US");
        _httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        _httpConnection.setRequestProperty("Content-Length",new Integer(postDataBytes.length).toString());
        os = _httpConnection.openOutputStream();
        os.write(_postDataBytes);

        int rc = _httpConnection.getResponseCode();
        if(rc == HttpConnection.HTTP_OK) {
              is = _httpConnection.openInputStream();
              is.read(_data);
        } else {
          _data = null;
        }
   } catch(Exception e) {
   }
   return (new String(_data));
}

}

/////////////////////////////////////////////// ////////////////////////////////

package mypackage;

import net.rim.blackberry.api.browser.URLEncodedPostData;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen implements FieldChangeListener
{
URLEncodedPostData _postData;
EditField urlField;
ButtonField connectButton;
RichTextField resultField;

public MyScreen()
{ 
// Set the displayed title of the screen 
setTitle("MyTitle");
_postData = new URLEncodedPostData("",false);
_postData.append("test","value");

urlField = new EditField("Url: ", " ");

resultField = new RichTextField();

connectButton = new ButtonField("Connect");
connectButton.setChangeListener(this);

add(connectButton);
add(urlField);
add(resultField);

}

public void fieldChanged(Field field, int context) {
if (field == connectButton) {
HttpPost hp = new HttpPost(urlField.getText(), _postData);
String result = hp.postData();
resultField.setText(result);
}
}
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////////// 我试图连接的网址是“http://www.chillicon.com.ng/scripts/posttest.php

if (isset($_POST['test'])) {
echo "Success!! ".$_POST['test'];
} else {
echo "Error";
}
?>

/////////////////////////////////////////////// ////////

请问我做错了什么?感谢

0 个答案:

没有答案