- Forum posts: 6
Aug 11, 2015, 8:19:11 AM via Website
Aug 11, 2015 8:19:11 AM via Website
I was reading some similar questions but could not find a solution to my Exception.
08-10 16:37:44.457: E/pass 1(2607): connection success
08-10 16:37:44.461: E/pass 2(2607): connection success
08-10 16:37:44.465: E/Fail 3(2607): org.json.JSONException: Value <meta of type java.lang.String cannot be converted to JSONObject
I get product_name and product_id from a listview and open on product_details activity. What I want now is to show a Toast message after clicking on a TextView to check product availability. After some tries I still get the exception above.
I´m not using an EditText to get data. I use a textView where I click to do the stuff. This could be the problem? Am I getting an empty string?
product_details.java
public class ProductDetails extends Activity {
TextView name;
String p_id, pname, yes, no;
InputStream inputStream = null;
String result = null;
String line = null;
int code;
private static final String TAG_PID = "product_id";
private static final String TAG_NAME = "product_name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
name = (TextView) findViewById(R.id.name);
Intent i = getIntent();
p_id = i.getStringExtra(TAG_PID);
pname = i.getStringExtra(TAG_NAME);
name.setText(pname);
final TextView et_yes = (TextView) findViewById(R.id.yes);
final TextView et_no = (TextView) findViewById(R.id.not_a);
et_yes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
yes = et_yes.getText().toString();
new insertYes().execute();
}
});
}
public class insertYes extends AsyncTask<String, Boolean, Boolean>{
@Override
protected Boolean doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("p_id", p_id));
nameValuePairs.add(new BasicNameValuePair("yes", yes));
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("my website link / insert_counter.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
inputStream = entity.getContent();
Log.e("pass 1", "connection success");
}catch(Exception e){
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP", Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
stringBuilder.append(line+"\n");
}
inputStream.close();
result = stringBuilder.toString();
Log.e("pass 2", "connection success");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
try {
JSONObject json_date = new JSONObject(result);
code = (json_date.getInt("code"));
if (code==1) {
Toast.makeText(getBaseContext(), "Insert success",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Sorry, try again",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e("Fail 3", e.toString());
}
return null;
}
}