Java 使用安卓系统发布HTTP帖子-“;println需要一条消息;错误
这是我的活动,我想用JSON格式的数据发送Post请求Java 使用安卓系统发布HTTP帖子-“;println需要一条消息;错误,java,android,json,http-post,Java,Android,Json,Http Post,这是我的活动,我想用JSON格式的数据发送Post请求 public class OrderActivity extends Activity { List<Piatto> ordine = new ArrayList<Piatto>(); Context context; @Override public void onCreate(Bundle savedInstanceState) {
public class OrderActivity extends Activity {
List<Piatto> ordine = new ArrayList<Piatto>();
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_layout);
TextView disptext = (TextView) findViewById(R.id.dispid);
ordine = getIntent().getParcelableArrayListExtra("ordinelist");
context = getApplicationContext();
String json = "";
Integer contatore = 0;
for (Integer i = 0; i < ordine.size(); i++) {
if (ordine.get(i) != null){
if (contatore > 0){
json = json+", ";
}
json = json+ordine.get(i).getJSON();
}
}
disptext.setText(json);
POST("http://some.altervista.org/order.php", json);
}
public static String POST(String url, String json){
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);
// 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");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
}
你对此有什么想法吗?我没有关于println的直接代码。
或者你对实施这样的计划还有其他建议吗
非常感谢这个问题也与内部调用println的Log类有关。例如,你有
Log.d("InputStream", e.getLocalizedMessage());
如果
e.getLocalizedMessage()
返回,您的应用程序将崩溃,出现异常情况println由log.d调用,您调用它。看看stacktrace。您的异常没有本地化消息。这不是记录异常的好方法,应该改用printStackTrace。您可能会看到它是一个networkonmainthreadexception,这在很大程度上已经有文档记录了。也不要犹豫,直接从您喜欢的IDE使用调试工具。您知道为什么它不发送有效的http请求吗?非常感谢,networkonMainthreadExecution
可能导致应用程序崩溃。尝试打印Log.d(“InputStream”,“Error”,e)
或e.printStackTrace()
以查看故障的真正原因exception@Blackbelt你能告诉我为什么你把我的问题标为重复的吗。在这样做之前,你必须先解决这个问题。你只是看了一下logcat并将其标记为重复。但我的问题有一个不同的问题。你可以建议我看看这个问题。我会很感激的
Log.d("InputStream", e.getLocalizedMessage());