I'm trying to make an android app using fuzzy (have yet to make the whole fuzzy calculation) so the calculations will be done in php. For starter I was just making a simple calculation in php first and trying to send to the android. but it doesn't show anything..there no error either.
Here's the simple example of my php code calc.php
<?php
$calcresult = 56 * 100 * 2051 / 49;
echo json_encode($calcresult);
?>
and this is my java code JSONActivity.class
package com.example.ta2;
import java.io.BufferedReader;
import java.io.InputStream;import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class AturanKonsumsi extends Activity {
private JSONObject jObject;
private String xResult ="";
private String url = "http://10.0.2.2/calc.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftarmakanan);
TextView txtResult = (TextView)findViewById(R.id.TextViewResult);
xResult = getRequest(url);
try {
parse(txtResult);
} catch (Exception e) {
e.printStackTrace();
}
}
private void parse(TextView txtResult) throws Exception {
jObject = new JSONObject(xResult);
JSONArray menuitemArray = jObject.getJSONArray("calcresult");
String sret="";
for (int i = 0; i < menuitemArray.length(); i++) {
System.out.println(menuitemArray.getJSONObject(i)
.getString("calcresult").toString());
sret +=menuitemArray.getJSONObject(i).getString(
"calcresult").toString()+"
";
}
txtResult.setText(sret);
}
public String getRequest(String Url){
String sret="";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(Url);
try{
HttpResponse response = client.execute(request);
sret =request(response);
}catch(Exception ex){
Toast.makeText(this,"Gagal "+sret, Toast.LENGTH_SHORT).show();
}
return sret;
}
public static String request(HttpResponse response){
String result = "";
try{
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
str.append(line + "
");
}
in.close();
result = str.toString();
}catch(Exception ex){
result = "Error";
}
return result;
}
}
when I run the android app it doesn't show the value in $calcresult and there's no error either. thanks for your help