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.