Reputation: 314
I tried to make my code to be like this: https://github.com/jbeerdev/BrightHubCode/blob/master/src/com/bright/hub/background/WebServiceBackgroundActivity.java This is how I call and send with the Async class, that seems to be fine. If I put all the code in my MainActivity it just works, but I can't seem to get this code via a external class to work.
My MainActivity code: (parts which matter)
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();
}
}
But on the Async class: https://github.com/jbeerdev/BrightHubCode/blob/master/src/com/bright/hub/background/WebServiceAsyncTask.java ^I get these kinds of errors: "java.lang.String[] cannot be cast to java.lang.String" So where this happens: "String serviceUrl = (String) params[0];" is where I have problems..? It's in the "doInBackground" at least: "An error occured while executing doInBackground()"
My Async class code: (parts which matter)
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;
}
The apps crashes on boot giving a Dialog it crashed. Thanks in advance for the help!
Edit My logcat: http://pastebin.com/XZaNZTTg
Upvotes: 0
Views: 176
Reputation: 2773
You have two significant lines in the crash log:
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)
Somewhere you're trying to use a string array as a single string, it would look as if it's here
String theurl = (String) params[0]; //urlJSON;
params[0]
is an array.
Upvotes: 1