Java HTTP Post请求Android
我正在尝试联系Android的API 我发现这个示例代码Java HTTP Post请求Android,java,php,android,api,http-post,Java,Php,Android,Api,Http Post,我正在尝试联系Android的API 我发现这个示例代码 public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { // Add your data
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/script.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”,“AndDev很酷!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
我的问题是,我如何才能看到反应,以确保它得到一个?有没有办法添加API密钥
另外,“yoursite.com”必须是PHP文件吗?观看这些视频
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
//read stream
//if expect binary data:
BufferedInputStream stream = new BufferedInputStream(instream);
int maxBytes = 128 * 1024;
if(entity.getContentLength() > maxBytes) {
throw new IllegalArgumentException("Much too big!");
}
byte[] bytes = new byte[(int) entity.getContentLength()];
int offset = 0, count = bytes.length;
while(stream.read(bytes, offset, count) > -1 && count > 0) {
count -= offset;
}
final Bitmap responseImage = BitmapFactory.decodeByteArray(
bytes, 0, bytes.length);
//or if you expect text
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream, Charset.forName(
entity.getContentEncoding().getValue())));
StringBuffer buffer = new StringBuffer();
String line;
while((line = reader.readLine()) != null) {
buffer.append(line);
}
final String responseText = buffer.toString();
} finally {
instream.close();
}
}
这是一个由6部分组成的android登录应用程序教程。
它还展示了如何使用MySQL创建数据库。希望有帮助。观看这些视频
这是一个由6部分组成的android登录应用程序教程。
它还展示了如何使用MySQL创建数据库。希望能有所帮助。服务器不必使用php,您可以在服务器端随意使用。我们使用python和Django,服务器不必使用php,在服务器端使用您想要的任何东西。我们使用python和Django。