0

I have this Android code:

package com.XXX

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

public class DataProvider {

    String baseUri;

    DataProvider() {
        baseUri = "http://www.XXX.de/XXX/";
    }

    public String requestUser(String userName) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet request = new HttpGet(baseUri + "get.php?userName=" + userName);
        ResponseHandler<String> handler = new BasicResponseHandler();

        String result = "";

        try {
            result = httpclient.execute(request, handler);
        } catch (ClientProtocolException e) {
            result = "ClientProtocolException was thrown";
        } catch (IOException e) {
            result = "IOException was thrown";
        }
        finally {
            httpclient.getConnectionManager().shutdown();
        }

        return result;
    }

}

and in my activity

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_reload_package) {
            String response = dataProvider.requestUser("BKDJSHDD-1912772-DIKDS-19172");
            browser.loadDataWithBaseURL("file:///android_asset/", response, "text/html", "UTF-8", "file:///android_asset/index.html");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

Clicking the menu item causes the app to Crash. The objects browser and dataProvider are 100% correctly declared. I test the app on my device, because my PC is to weak to run the emulator, therefore no console output.

Any ideas?:..

Liglo App
  • 3,719
  • 4
  • 30
  • 54
  • Check the constructor for dataprovider. There is a spelling mistake – Msk Apr 13 '15 at 17:10
  • 2
    "therefore no console output" -- you can get at LogCat from hardware as well as from emulators. In fact, it's the same process. – CommonsWare Apr 13 '15 at 17:11
  • Based on the code currently provided, it looks like you're running the network request on the main thread. [You can't do that](http://stackoverflow.com/q/6343166/390989). – eldarerathis Apr 13 '15 at 17:13
  • True, but he should be catching that crash right? – Eric S. Apr 13 '15 at 17:16

1 Answers1

2

My first impression is the bad declaration of HTTP Request. In Android if you want to make an HttpRequest you must put this in other thread, not in the main thread.

Your DataProvider.java is OK in terms of declaration and null checks but doesn't meet one of the safety standards Android android.os.NetworkOnMainThreadException.

Run yout HTTP Request with AsyncTask, something like this in your Activity:

// The other part of your code ....

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_reload_package) {
        new DataProviderAsyncTask().execute();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

class DataProviderAsyncTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String response = dataProvider
                .requestUser("BKDJSHDD-1912772-DIKDS-19172");
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        browser.loadDataWithBaseURL("file:///android_asset/", result,
                "text/html", "UTF-8", "file:///android_asset/index.html");
    }

}

I hope you serve

Cesardl
  • 1,831
  • 1
  • 14
  • 18