我无法说明为什么我的代码没有得到对帖子请求的回复。这是服务器的文档:
EMT-REST (JSON)访问的Web服务
所有服务请求都被称为POST,因此所有参数都应添加到正文请求中
响应是JSON格式的文本
所有方法请求都需要 idClient 和 passKey ,这些值由EMT根据请求提供。
网址为https://openbus.emtmadrid.es:9443/emt-proxy-server/last/
GetArriveStop POST
$ {SERVERURL} /geo/GetArriveStop.php
获取巴士到达信息到目标站点
请求
idClient API客户端标识符字符串
passKey API客户端密码字符串
idStop停止id字符串 统计私人使用(不设置)字符串
cultureInfo EN | ES响应字符串的语言
响应
到达抵达名单
最后,这是我执行它的Android代码:
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import com.hmkcode.android.R;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
EditText etName;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
etName = (EditText) findViewById(R.id.etName);
new HttpAsyncTask().execute("https://openbus.emtmadrid.es:9443/emt-proxy-server/last/geo/GetArriveStop.php");
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent and received!", Toast.LENGTH_LONG).show();
etName.setText(result); //editText from layout
}
}
public static String POST(String url){
Log.i("METHOD POST INICIATED", "METHOD POST");
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("idClient","EMT.SERVICIOS.EMTJSONBETA");
jsonObject.accumulate("passKey", "4F4EEB75-A822-41E3-817B-AB301D5DA321");
jsonObject.accumulate("idStop", "488");
jsonObject.accumulate("cultureInfo", "ES");
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternatives way to convert Person object to JSON string usin Jackson Lib
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
Log.i("HTTP EXECUTION", "right before line httpclient.exeute");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
Log.i("AFTER HTTP EXECUTION", "right before line hhtpclient.exeute");
// 9. receive response as inputStream
//inputStream = httpResponse.getEntity().getContent();
result = EntityUtils.toString(httpResponse.getEntity());
// 10. convert inputstream to string
//if(inputStream != null){
Log.i("CONVERT INPUT", "convertInputStreamToString");
//result = convertInputStreamToString(inputStream);
//}else{
Log.i("NO INPUT", "result = did not work!");
//result = "Did not work!";
//}
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
}
在这行代码之后它不再继续了 // 8.对给定的URL执行POST请求 HttpResponse httpResponse = httpclient.execute(httpPost);
我不知道为什么。
我已经尝试过以另一台服务器为例,它工作正常,也许我在标题中遗漏了一些东西。服务器的完整文档:http://blog.emtmadrid.es/wp-content/uploads/2014/05/API-REST-JSON-v-1.1.rar