0

I have two classes one is called Main1843 and the other is JsonParse. When i copy everything in same class as main1843 everything works just fine. but i prefer having split classes and being tidy. The problem is i cant run the code below. No errors displayed, just app stops running. (Manifest file has permissions)

Since it is a short program maybe you can examine each line.

Main1843 class:

public class Main1843 extends Activity implements View.OnClickListener {
private Button button;
private TextView tv;
private EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1843);

    tv = (TextView) findViewById(R.id.panel);
    et = (EditText) findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(this);
}

public void onClick(View v) {
    String text;
    text = et.getText().toString();
    tv.append(text);
    tv.append("\n");
    new JsonParse().execute();
}

protected void putScreen(String t) {
    tv.append(t);
}   
}

And the other class(JsonParse) which extends AsyncTask<> ;

public class JsonParse extends AsyncTask{
private String yazi;
protected String yazi2;
protected String id;

private String TAG = "JWP";


@Override
protected Object doInBackground(Object[] objects) {
    return POST("http://somewebsite/show.php");
}


public String POST(String url) {
    String result = "";
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    Main1843 m = new Main1843();

    try {
        HttpResponse response = client.execute(post);
        InputStream inputStream = response.getEntity().getContent();
        if (inputStream != null) {
            String text = "";
            result = convertInputStreamToString(inputStream);
            Log.d(TAG, result);
            JSONArray reader = new JSONArray(result);
            for (int i = 0; i < reader.length(); i++) {
                JSONObject data = reader.getJSONObject(i);
                id = data.getString("id");
                yazi = data.getString("yazi");
                yazi2 = data.getString("yazi2");

                text += yazi;
                text += " ";
            }
            m.putScreen(text);
        } else
            result = "Did not work";
        return result;
    } catch (IOException e) {
        e.printStackTrace();
        return "Exception 1";
    } catch (Exception e) {
        e.printStackTrace();
        return "haha";
    }
}


private String convertInputStreamToString(InputStream inputStream) {
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    String result = "";

    try {
        while ((line = br.readLine()) != null)
            result += line;
        br.close();
        return result;
    } catch (IOException e) {
        e.printStackTrace();
        return "Exception";
    }

}
}

Thank you for your time.

  • Do you add the internet permission? – Chefes Dec 03 '14 at 17:43
  • Yes, already wrote that, manifest file has internet permission and internet state permission. –  Dec 03 '14 at 17:45
  • `Main1843 m = new Main1843();` this is not how you use activities – Ken Wolf Dec 03 '14 at 17:50
  • Ok, why you are not using gson? – Chefes Dec 03 '14 at 17:51
  • @KenWolf can you be more specific? i'm using that for accessing "putScreen" function. –  Dec 03 '14 at 17:52
  • @user3586222 i want to finish this program by using json. next time i'll try gson –  Dec 03 '14 at 17:52
  • You can only start an activity via the intent system, you cannot instantiate it yourself. http://developer.android.com/training/basics/firstapp/starting-activity.html – Ken Wolf Dec 03 '14 at 17:55
  • My intent is not to start an activity, just want to use the function which appears in Main class. how am i supposed to make connection? –  Dec 03 '14 at 17:57
  • You will need to pass your activity context into your AsyncTask to call methods on it, or you can call a listener that your activity implements, or you can use a localbroadcast. – Ken Wolf Dec 03 '14 at 18:00
  • For connect your MainActivity you can use Singleton pattern or pass a Activity parameter. – Chefes Dec 03 '14 at 18:00
  • @KenWolf can you post this as an answer please. thanks –  Dec 03 '14 at 18:01
  • You can not directly create object of Main1843 in your JSonParse class. This is what you can try out if you want to have public class for your AsyncTask implementation. http://stackoverflow.com/questions/18479366/updating-ui-thread-from-asynctask-in-public-class – Sariq Shaikh Dec 03 '14 at 18:37

1 Answers1

0

Comments solved the problem

"You will need to pass your activity context into your AsyncTask to call methods on it, or you can call a listener that your activity implements, or you can use a localbroadcast." KenWolf

@ShaikhMohammedShariq shared a similar problem answer

Community
  • 1
  • 1