我试图让我的代码是这样的: https://github.com/jbeerdev/BrightHubCode/blob/master/src/com/bright/hub/background/WebServiceBackgroundActivity.java 这就是我使用异步调用和发送的方式课,好像没问题。如果我将所有代码都放在我的 MainActivity 中,它就可以工作,但我似乎无法通过外部类让这些代码工作。
我的 MainActivity 代码:(重要的部分)
public class MainActivity extends FragmentActivity implements ActionBar.OnNavigationListener {
private static final String url = "http://www.somesite.com/JSON.php";
public String post;
public ListView listView;
public String jsonResult;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeDialog();
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
accessWebService();
}
private void accessWebService() {
JsonReadTask jsonTask = new JsonReadTask();
jsonTask.execute(new String[] { url, post },this);
}
private void initializeDialog() {
dialog = ProgressDialog.show(MainActivity.this, "", "LoadingData. Wait...", true);
dialog.show();
}
public void ListDrawer() {
List<Map<String, String>> galleryList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("galleries");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String loc = jsonChildNode.optString("loc");
String foto = jsonChildNode.optString("foto");
String outPut = "http://www.somesite.com/images/" + loc + foto;
galleryList.add(createGallery("galleries", outPut));
}
} catch (JSONException e) {
//Toast.makeText(getApplicationContext(), "Error" + e.toString(),
//Toast.LENGTH_LONG).show();
}
}
但在异步类: https ://github.com/jbeerdev/BrightHubCode/blob/master/src/com/bright/hub/background/WebServiceAsyncTask.java ^我得到这些类型的错误:“java.lang.String[ ] 不能转换为 java.lang.String" 所以发生这种情况:"String serviceUrl = (String) params[0];" 我在哪里有问题..?它至少在“doInBackground”中:“执行 doInBackground() 时发生错误”
我的异步类代码:(重要的部分)
public class JsonReadTask extends AsyncTask<Object, Void, String> {
MainActivity callerActivity;
@Override
public String doInBackground(Object... params) {
callerActivity = (MainActivity) params[1];
HttpClient httpclient = new DefaultHttpClient();
String theurl = (String) params[0]; //urlJSON
HttpPost httppost = new HttpPost(theurl);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
String thepost = (String) params[1]; //tablename
nameValuePairs.add(new BasicNameValuePair("getgallery", "thepost"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
callerActivity.jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
应用程序在启动时崩溃,给出一个崩溃的对话框。在此先感谢您的帮助!
编辑我的 logcat:http://pastebin.com/XZaNZTTg