• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help Send value to json php webservice android

  • Thread starter Thread starter Android Question
  • Start date Start date
A

Android Question

Guest
I'm trying to insert edittext values to a mysql table using json and a PHP webservice. I found code to do this; it should pass "success" message when the webservice is working. But it force close when the submit button is clicked. This is the code:

protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mhost=(EditText) findViewById(R.id.field_host);
mdb=(EditText) findViewById(R.id.field_db);
muser=(EditText) findViewById(R.id.field_user);
mpassword=(EditText) findViewById(R.id.field_password);

mConnection=(Button) findViewById(R.id.insert);
mConnection.setOnClickListener(newOnClickListener(){

@override
publicvoid onClick(View v){
// TODO Auto-generated method stub
newConnect().execute();

}
});
}
classConnectextendsAsyncTask<String,String,String>{

@override
protectedString doInBackground(String... args){
// TODO Auto-generated method stub
String HOST=mhost.getText().toString();
String USER=muser.getText().toString();
String DB=mdb.getText().toString();
String PASSWORD=mpassword.getText().toString();

List<NameValuePair> params=newArrayList<NameValuePair>();
params.add(newBasicNameValuePair("nfc_tag", HOST));
params.add(newBasicNameValuePair("meter_reading", USER));
params.add(newBasicNameValuePair("reading_datetime", DB));
params.add(newBasicNameValuePair("image_name", PASSWORD));


JSONObject json=jp.makeHttpRequest(connection_url,"POST", params);

try{

int success=json.getInt(Tag_success);
if(success==1){

Toast.makeText(getApplicationContext(),"Connected :-)",Toast.LENGTH_LONG);
}else{

Toast.makeText(getApplicationContext(),"Not Connected :(",Toast.LENGTH_LONG);
}

}catch(JSONException e){
// TODO: handle exception
e.printStackTrace();
}
returnnull;
}

}


-JsonParser class-

publicclassJsonParser{

staticInputStream IS=null;
staticJSONObject JB=null;
staticString json="";

publicJsonParser(){


}
publicJSONObject makeHttpRequest(String url,String method,List<NameValuePair> params){
try{

if(method=="POST"){

DefaultHttpClient httpClient=newDefaultHttpClient();
HttpPost httpPost=newHttpPost(url);
httpPost.setEntity(newUrlEncodedFormEntity(params));

}elseif(method=="GET"){
DefaultHttpClient httpClient=newDefaultHttpClient();
String paramaString =URLEncodedUtils.format(params,"utf-8");
url +="?"+paramaString;
HttpGet httpGet=newHttpGet(url);

HttpResponse httpresponse=httpClient.execute(httpGet);
HttpEntity httpEntity=httpresponse.getEntity();
IS=httpEntity.getContent();

}

}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch(ClientProtocolException e){
// TODO: handle exception
}catch(IOException e){
// TODO: handle exception
e.printStackTrace();
}

try{

BufferedReader reader=newBufferedReader(newInputStreamReader(IS,"iso-8859"),8);
StringBuilder sb=newStringBuilder();
String line=null;
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
IS.close();
json=sb.toString();

}catch(Exception e){
// TODO: handle exception
Log.e("Buffer Error","Connection Error"+e.toString());
}
try{
JB=newJSONObject(json);
}catch(JSONException e){
// TODO: handle exception
Log.e("JSON Parser", e.toString());
}

return JB;
}

-PHP Script-

<?php
$host=$_POST['nfc_tag'];
$user=$_POST['meter_reading'];
$db=$_POST['reading_datetime'];
$password=$_POST['image_name'];

$response=array();
$connect=mysql_connect($host,$user,$password);

if(mysql_select_db($db,$connect)){

$response['success']=1;
}else{
$response['success']=2;
}
echo json_encode($response);
?>
 
Back
Top Bottom