這就是我的活動,我想要發送帶格式數據JSON的Post請求。HTTP Post with Android - 「println需要一條消息」錯誤
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;
}
}
那麼,這是錯誤我收到
07-23 16:09:21.450: E/AndroidRuntime(5806): FATAL EXCEPTION: main
07-23 16:09:21.450: E/AndroidRuntime(5806): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.sii.mywaiter/it.sii.mywaiter.OrderActivity}: java.lang.NullPointerException: println needs a message
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.ActivityThread.access$700(ActivityThread.java:134)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.os.Looper.loop(Looper.java:137)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.ActivityThread.main(ActivityThread.java:4867)
07-23 16:09:21.450: E/AndroidRuntime(5806): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 16:09:21.450: E/AndroidRuntime(5806): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 16:09:21.450: E/AndroidRuntime(5806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-23 16:09:21.450: E/AndroidRuntime(5806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-23 16:09:21.450: E/AndroidRuntime(5806): at dalvik.system.NativeStart.main(Native Method)
07-23 16:09:21.450: E/AndroidRuntime(5806): Caused by: java.lang.NullPointerException: println needs a message
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.util.Log.println_native(Native Method)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.util.Log.d(Log.java:155)
07-23 16:09:21.450: E/AndroidRuntime(5806): at it.sii.mywaiter.OrderActivity.POST(OrderActivity.java:133)
07-23 16:09:21.450: E/AndroidRuntime(5806): at it.sii.mywaiter.OrderActivity.onCreate(OrderActivity.java:71)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.Activity.performCreate(Activity.java:5047)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-23 16:09:21.450: E/AndroidRuntime(5806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
07-23 16:09:21.450: E/AndroidRuntime(5806): ... 11 more
你有關於一些想法?我沒有關於println的直接代碼。 或者你有關於實現類似的東西的其他建議?
非常感謝
的println被稱爲log.d,你叫崩潰。看看堆棧跟蹤。您的例外沒有本地化的消息。這不是記錄異常的好方法,您應該使用printStackTrace來代替。你可能會看到它是一個網絡主要的前景,已經有大量的文檔記錄。也不要猶豫,使用直接從您最喜愛的IDE訪問的調試工具。 – njzk2