这是我的主要活动: -
package com.example.shafqatsk.android_php_sql;
import org.json.JSONArray; import org.json.JSONException; import
org.json.JSONObject; import android.os.AsyncTask; import
android.os.Bundle; import android.app.Activity; import
android.view.Menu; import android.widget.TextView;
public class MainActivity extends Activity {
JSONObject jobj = null;
ServiceHandler sv = new ServiceHandler();
TextView itemName, itemColor, itemPrice;
String ab;
JSONArray jsonArray;
JSONObject jsonObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemName = (TextView) findViewById(R.id.textView1);
itemColor = (TextView) findViewById(R.id.textView2);
//start background processing
new RetreiveData().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
class RetreiveData extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
jobj = sv.makeHttpRequest("http://192.168.1.253/StockService.asmx/GetItemData");
try {
//jsonArray = jobj.getJSONArray(arg0[0]);
ab = jobj.getString("ItemName");
ab += jobj.getString("ItemId");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ab;
}
protected void onPostExecute(String ab){
itemName.append(ab);
}
} }
这是我的服务处理程序类: -
package com.example.shafqatsk.android_php_sql;
import java.io.BufferedReader; import java.io.IOException; import
java.io.InputStream; import java.io.InputStreamReader; import
org.apache.http.HttpEntity; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException; import
org.apache.http.client.methods.HttpPost; import
org.apache.http.impl.client.DefaultHttpClient; import
org.json.JSONException; import org.json.JSONObject; /* this class
helps to get data from server*/ public class ServiceHandler {
//input stream deals with bytes
static InputStream is = null;
static JSONObject jobj = null;
static String json = "";
//constructor
public ServiceHandler(){
}
//this method returns json object.
public JSONObject makeHttpRequest(String url){ //http client helps to send and receive data
DefaultHttpClient httpclient = new DefaultHttpClient(); //our request method is post
HttpPost httppost = new HttpPost(url);
try { //get the response
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity httpentity = httpresponse.getEntity(); // get the content and store it into inputstream object.
is = httpentity.getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try { //convert byte-stream to character-stream.
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while((line = reader.readLine())!=null){
sb.append(line+"\n");
} //close the input stream
is.close();
System.out.println("Result : "+sb.toString());
json = sb.toString();
try {
jobj = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jobj;
} }
Logcat出错: -
05-19 02:10:42.336 9552-9589/com.example.shafqatsk.android_php_sql E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.shafqatsk.android_php_sql.MainActivity$RetreiveData.doInBackground(MainActivity.java:48)
at com.example.shafqatsk.android_php_sql.MainActivity$RetreiveData.doInBackground(MainActivity.java:39)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
05-19 02:10:42.352 9552-9552/com.example.shafqatsk.android_php_sql E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
05-19 02:10:42.356 9552-9552/com.example.shafqatsk.android_php_sql E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
答案 0 :(得分:1)
我想问更多细节,但我没有许可,所以我写在这里。
也许您的jobj
为空
System.out.println("Result : "+sb.toString());
的输出是什么?
答案 1 :(得分:1)
如果需要,您在请求中传递什么参数?如果您的网络服务类型是发布的。
您可以使用loopj库http://loopj.com/android-async-http/而不是为Web调用执行代码,而是在此库中处理所有异常。您只需要使用JSON密钥解析数据表单响应,您就可以获得该站点的指南。我希望这能帮到您。