我试着让我的代码像这样: https://github.com/jbeerdev/BrightHubCode/blob/master/src/com/bright/hub/background/WebServiceBackgroundActivity.java 这就是我用Async类调用和发送的方式,这看起来很好。 如果我将所有代码放在我的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();
}
}
但是在Async类上: 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;
}
应用程序在启动时崩溃,导致Dialog崩溃。 在此先感谢您的帮助!
编辑 我的logcat:http://pastebin.com/XZaNZTTg
答案 0 :(得分:1)
崩溃日志中有两条重要的行:
10-13 11:49:43.881: E/AndroidRuntime(7289): Caused by: java.lang.ClassCastException: java.lang.String[] cannot be cast to java.lang.String
10-13 11:49:43.881: E/AndroidRuntime(7289): at com.example.providenceartapp.JsonReadTask.doInBackground(JsonReadTask.java:31)
在某处您尝试将字符串数组用作单个字符串,它看起来就好像在这里
String theurl = (String) params[0]; //urlJSON;
params[0]
是一个数组。