I have a link where I'm providing 2 parameters and a php server side script is executing a query and writing them to my databae.
The problem is that in this specific case, it seems that I can't connect to teh url.
Here is my button xml file:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="#+id/btnSubmit"
android:text="#string/btnSubmit"
android:onClick="submitNewJ"
/>
Here is my click "listener":
public void submitNewJ(View view){
new submitJ().execute();
}
And here is the submitJ code:
public class submitJ extends AsyncTask<Void, Integer, Void>{
#Override
protected Void doInBackground(Void... params) {
try{
String encodedName = URLEncoder.encode(Name,"UTF-8");
String encodedBody = URLEncoder.encode(Body,"UTF-8");
URL url = new URL("http://site123.com/android/J/sJ.php?Name="+encodedName+"&Body="+encodedBody);
URLConnection urlConnection = url.openConnection();
#SuppressWarnings("unused")
InputStream in = urlConnection.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
And here is how I'm getting the strings:
EditText jN;
EditText jB;
String Name = "";
String Body = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
jN = (EditText)findViewById(R.id.newName);
jB = (EditText)findViewById(R.id.newBody);
Name = jN.getText().toString();
Body = jB.getText().toString();
}
It seems that the connection is not working here(even when I'm using the same code in otehr activities). Where am I mistaking?
I know that I'm missing something super small, but I'm not able to spot it.
P.s. My service and link are 100% tested and working.
The site you have in the example is probabbly for reference reasons, but mind that if it is using https protocol and you're reffering to a http, you will not get redirected to the correct link.
Everything in your code seems fine to me.
Just make sure that you're using the correct protocol.
You need to move these lines from onCreate() method to doInBackground() method,
Name = jN.getText().toString();
Body = jB.getText().toString();
put them inside the doInBackground() method like below,
public class submitJ extends AsyncTask<Void, Integer, Void>{
#Override
protected Void doInBackground(Void... params) {
try{
String Name = jN.getText().toString(); // move here and delete from the class
String Body = jB.getText().toString();
String encodedName = URLEncoder.encode(Name,"UTF-8");
String encodedBody = URLEncoder.encode(Body,"UTF-8");
URL url = new URL("http://site123.com/android/J/sJ.php?Name="+encodedName+"&Body="+encodedBody);
URLConnection urlConnection = url.openConnection();
#SuppressWarnings("unused")
InputStream in = urlConnection.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
Related
I have been trying to get the walking duration and distance between two locations using Distance Matrix API. I want to store this results (Distance and Duration) in variables that I will be able to access from other classes/activities.
I managed to get time and duration using AsyncTask, but when I try to store them in a variable in PostExecute method, they are always unassigned when accessing from a method or class outside of the PostExecute method. I now, understand how AsyncTask works and that is asynchronous. I tried to implement interfaces and then try to access the data but still I was not able to.
GeoTask is an inner class in my Map class.
public class GeoTask extends AsyncTask<String, Void, String> {
ProgressDialog pd;
Context mContext;
SelectTime selectTime;
//constructor is used to get the context.
public GeoTask(Context mContext) {
this.mContext = mContext;
}
public GeoTask() {
}
//This function is executed before before "doInBackground(String...params)" is executed to dispaly the progress dialog
#Override
protected void onPreExecute() {
super.onPreExecute();
selectTime=new SelectTime();
pd=new ProgressDialog(mContext);
pd.setMessage("Loading");
pd.setCancelable(false);
pd.show();
}
//This function is executed after the execution of "doInBackground(String...params)" to dismiss the dispalyed progress dialog and call "setDouble(Double)" defined in "MainActivity.java"
#Override
protected void onPostExecute(String aDouble) {
super.onPostExecute(aDouble);
if(aDouble!=null)
{
setDouble(aDouble);
}
else
Toast.makeText(mContext, "Error4!Please Try Again with proper values", Toast.LENGTH_SHORT).show();
}
#Override
protected String doInBackground(String... params) {
try {
URL url=new URL(params[0]);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
int statuscode=con.getResponseCode();
if(statuscode==HttpURLConnection.HTTP_OK)
{
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=br.readLine();
while(line!=null)
{
sb.append(line);
line=br.readLine();
}
String json=sb.toString();
Log.d("JSON",json);
JSONObject root=new JSONObject(json);
JSONArray array_rows=root.getJSONArray("rows");
Log.d("JSON","array_rows:"+array_rows);
JSONObject object_rows=array_rows.getJSONObject(0);
Log.d("JSON","object_rows:"+object_rows);
JSONArray array_elements=object_rows.getJSONArray("elements");
Log.d("JSON","array_elements:"+array_elements);
JSONObject object_elements=array_elements.getJSONObject(0);
Log.d("JSON","object_elements:"+object_elements);
JSONObject object_duration=object_elements.getJSONObject("duration");
JSONObject object_distance=object_elements.getJSONObject("distance");
Log.d("JSON","object_duration:"+object_duration);
return object_duration.getString("value")+","+object_distance.getString("value");
}
} catch (MalformedURLException e) {
Log.d("error", "error1");
} catch (IOException e) {
Log.d("error", "error2");
} catch (JSONException e) {
Log.d("error","error3");
}
return null;
}
}
In Map class, the setDouble method:
public void setDouble(String result) {
String res[] = result.split(",");
Double m = Double.parseDouble(res[0]) / 60;
Double d = Double.parseDouble(res[1])/1000;
minutes=m
distance=d
}
The minutes and distance variables initialisation
private double minutes;
public double getMinutes() {
return minutes;
}
private double distance;
public double getDistance() {
return distance;
}
and the execution:
LatLng destinationLatLng = getLatLngFromAddress(destinationPassed);
currentOrigin = getAddressFromLatLng(currentLat,currentLong);
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + currentOrigin + "&destinations=" + destinationPassed + "&mode=walking&language=fr-FR&avoid=tolls&key=API_KEY";
Log.d("url string",url);
geoTask.execute(url);
Furthermore, I try to access the minutes and distance variables using the get methods in another class and I get the value 0.0 returned all the time, even after the PostExecution is completed (I tested that it completed using Logs)
1)How can I achieve what I want? Access the distance and duration returned by the API in different classes? It is essential for the app that I am doing to achieve that.
2)If I cant achieve what I want using AsyncTask and this method, is there any way to use Distance Matrix API without using AsyncTask?
Probably you should replace in your
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + currentOrigin + "&destinations=" + destinationPassed + "&mode=walking&language=fr-FR&avoid=tolls&key=API_KEY";
line API_KEY characters by your valid Distance API key.
Take a look at this guide to get information how to obtain valid API key.
I am developing an Android Application (Android Studio - Java) which includes a sign in and registration process. I took care of the sign in and registration processes by implementing a connection between PHP files and a MySQL database through http. In short, I just created an AsyncTask class in java --called from another class-- and used it to post data to a PHP file, from there I just used the appropriate SQL commands. This part works fine.
This first part with the login and registration is important because every user will see a slightly different layout once they login. The layout is a RecyclerView composed of detailed CardView elements. Each CardView has a few TextViews with some details. The details are held by an Object i created in a separate class. To fill in the CardView elements a fetched some JSON data using a separate PHP file (and one more http connection). Parsing the data from JSON into Strings and ints was a straightforward endeavor, as was adding them to the list of custom objects. There is some code below showing how I fetched the data and added it to the Object list.
This is the complete AsyncTask class:
public class ScheduleWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
ScheduleWorker (Context ctx) { context = ctx; }
#Override
protected void onPreExecute() { super.onPreExecute(); }
#Override
protected void onPostExecute (String s) {
super.onPostExecute(s);
Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
try {
loadJSON(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected String doInBackground(String... params) {
final String fetch_url = "http://192.168.1.70/newfetcher.php";
try {
String ussr_name = params[0];
URL url = new URL(fetch_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream outputStream = con.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(ussr_name, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
private void loadJSON(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
Course course;
List<Course> courses = new ArrayList<Course>();
int len = jsonArray.length();
String[] titles = new String[len];
String[] types = new String[len];
String[] teachers = new String[len];
int[] pics = new int[len];
for (int i = 0; i < len; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
titles[i] = obj.getString("title");
types[i] = obj.getString("type");
teachers[i] = obj.getString("teacher");
pics[i] = obj.getInt("pic");
course = new Course(titles[i], types[i], teachers[i], pics[i]);
courses.add(course);
}
}
}
Now, where I encountered problems is when I tried saving the List of Objects (and their details) to a Room Persistent Database. I based my code for the Room Database off an example from Google Developer CodeLabs. I adapted the code for my particular needs but I kept the underlying class structure. The structure includes: an Entity, a DAO, a RoomDatabase, a Repository, a ViewModel, a ViewHolder, an Adapter for the RecyclerView, and a class to populate the database. Everything seems fine except for the part where I populate the database. The example populates the database by using a callback and an AsyncTask within the RoomDatabase class.
Here is populating AsyncTask:
private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {
#Override
public void onOpen(#NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
// If you want to keep the data through app restarts,
// comment out the following line.
new PopulateDbAsync(INSTANCE).execute();
}
};
/**
* Populate the database in the background.
* If you want to start with more words, just add them.
*/
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private final WordDao mDao;
PopulateDbAsync(WordRoomDatabase db) {
mDao = db.wordDao();
}
#Override
protected Void doInBackground(final Void... params) {
// Start the app with a clean database every time.
// Not needed if you only populate on creation.
Word word = new Word("Hello");
mDao.insert(word);
word = new Word("World");
mDao.insert(word);
return null;
}
}
My question is how do I populate the database with the detail arrays and/or the Object list? The example executes the callback every time a adding activity is called. I just want to populate the database when the user registers.
I developing android app and now I have problem. Below is a part of my code, and it keeps skipping the "for" part. When I put a breakpoint inside for statement, it stops at the point, and executes the lines very well and makes an output that I want. When I just 'run' app, it skips that part so "String locations" value doesn't change. I googled and some say it's thread-related problem. So I put synchroinzed on the method, still not working. Any other suggestions?
UPDATE
I was trying to show code only related to the problem, but I think now showing the whole would be more useful for those who try to help so here's my entire code on showMapActivity. You can see I've tried some ways around and nothing worked. Saving path's information into String url is where I'm having problem. I tested, and other parts seem to work fine. I know my code is really massy, that was why I only posted parts of the code. TMap related classes are imported from .jar file.
public class showMapActivity extends Activity {
TMapData tmapdata=new TMapData();
TMapView tmapView;
TMapPoint origin, dest;
volatile ArrayList<TMapPoint> points=new ArrayList<>();
private TextView x;
private TextView y;
private HashMap<String,LatLng> coordinates;
private HashMap<LatLng,Double> finalpoint;
static private ConcurrentHashMap<Double,Double> path;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_map);
coordinates=new HashMap<>();
Intent intent=getIntent();
tmapView=new TMapView(this);
path=new ConcurrentHashMap<>();
coordinates=(HashMap<String,LatLng>)intent.getSerializableExtra("coordinate");
path=getPathPoints(coordinates);
int i=0;
String url=getUrl();
//String url = "https://maps.googleapis.com/maps/api/elevation/json?locations=";
//String locations="";
/*
Iterator<Double> keys= path.keySet().iterator();
while(keys.hasNext()){
Double key=keys.next();
//String lat=String.valueOf(key);
//String lng=String.valueOf(path.get(key));
locations=locations+String.valueOf(key)+","+String.valueOf(path.get(key));
if(keys.hasNext())
locations=locations+"|";
}path.entrySet()
*/
/*
for(ConcurrentHashMap.Entry<Double,Double> elem : path.entrySet())
{
String lat=String.valueOf(elem.getKey());
String lng=String.valueOf(elem.getValue());
locations=locations+lat+","+lng;
i++;
if(i!=path.size())
{
locations=locations+"|";
}
}
*/
//url=url+locations+"&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto";
//String url=getUrl(path);
FetchUrl fetchUrl=new FetchUrl();
fetchUrl.execute(url);
}
private ConcurrentHashMap<Double,Double> getPathPoints(HashMap<String,LatLng> coordinates)
{
final ConcurrentHashMap<Double,Double> Path=new ConcurrentHashMap<>();
tmapView.setSKPMapApiKey("6bb5b7f3-1274-3c5e-ba93-790aee876673");
origin=new TMapPoint(coordinates.get("origin").latitude,coordinates.get("origin").longitude);
dest=new TMapPoint(coordinates.get("dest").latitude,coordinates.get("dest").longitude);
tmapdata.findPathData(origin, dest, new TMapData.FindPathDataListenerCallback() {
#Override
public void onFindPathData(TMapPolyLine polyLine) {
points=polyLine.getLinePoint();
for(TMapPoint point : points )
Path.put(point.getLatitude(),point.getLongitude());
}
});
return Path;
}
//ConcurrentHashMap<Double,Double> path
private synchronized String getUrl() {
int i=0;
String url = "https://maps.googleapis.com/maps/api/elevation/json?locations=";
String locations="";
for(HashMap.Entry<Double,Double> elem : path.entrySet())
{
String lat=String.valueOf(elem.getKey());
String lng=String.valueOf(elem.getValue());
locations=locations+lat+","+lng;
i++;
if(i!=path.size())
{
locations=locations+"|";
}
}
url=url+locations+"&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto";
//https://maps.googleapis.com/maps/api/elevation/json?locations=
// 39.7391536,-104.9847034|36.455556,-116.866667&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto
// Output format
return url;
}
private class FetchUrl extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try {
// Fetching the data from web service
//downloadURL
data = downloadUrl(url[0]);
Log.d("Background Task data", data.toString());
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//ParserTask
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
//읽은 데이터를 버퍼에 저장
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
Log.d("downloadUrl", data.toString());
br.close();
} catch (Exception e) {
Log.d("Exception", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class ParserTask extends AsyncTask<String, Integer, ArrayList<Double>> {
// Parsing the data in non-ui thread
#Override
protected ArrayList<Double> doInBackground(String... jsonData) {
JSONObject jObject;
ArrayList<Double> altitude = null;
try {
jObject = new JSONObject(jsonData[0]);
Log.d("ParserTask",jsonData[0].toString());
//DataParser class 호출
DataParser parser = new DataParser();
Log.d("ParserTask", parser.toString());
// Starts parsing data
altitude = parser.parse(jObject);
Log.d("ParserTask","Getting Altitudes");
Log.d("ParserTask",altitude.toString());
} catch (Exception e) {
Log.d("ParserTask",e.toString());
e.printStackTrace();
}
return altitude;
}
// Executes in UI thread, after the parsing process
#Override
protected void onPostExecute(ArrayList<Double> result) {
finalpoint=new HashMap<>();
LatLng latLng;
int i=0;
for(HashMap.Entry<Double,Double> elem : path.entrySet() )
{
latLng=new LatLng(elem.getKey(),elem.getValue());
finalpoint.put(latLng,result.get(i++));
}
x = (TextView) findViewById(R.id.textView5);
y = (TextView) findViewById(R.id.textView6);
x.setText(String.valueOf(finalpoint.get(coordinates.get("origin"))));
y.setText(String.valueOf(finalpoint.get(coordinates.get("dest"))));
}
}
}
(Apologies for posting this as an answer - I don't yet have the required reputation to comment)
Simply adding synchronized to a method doesn't necessarily guarantee thread safety.
How and when is path being populated?
Update after additional information provided
The problem seems to be that the path points are being generated asynchronously, and you are trying to use them before the generation process has finished (or perhaps even started). This happens because the findPathData simply starts the generation process and returns immediately (i.e. before the generation process has finished). In your code, you then go on and build the URL which is supposed to contain the point data immediately. At this point the background point generation process may not have finished, and may not have even started. As a result the point map may be empty or incomplete, and your URL will not be generated as you expect.
You need to find a way to wait until all of the path point data has been returned by the asynchronous processing before creating the URL. This looks like it could be very difficult, if not impossible, with the version of the findPathData method you are using, because it returns points via the callback one at a time and you may not know how many will be generated.
I had a quick look at the API for TMapData and it has a findPathDataAll method which seems to generate all the points and return them in a single callback call rather than one by one. If this is indeed what it does (sorry, I can't read Korean), you could use this method and then generate the URL from the callback, because when it's called you know that the generation process has been completed. If you do this, be careful to make sure that you're on the main thread before interacting with the UI or Activity.
Hope that helps.
First time using Android Studio in any large capacity. Using the code from here: https://stackoverflow.com/a/30937657/5919360, I was able to successfully pull the information I wanted from the URL, but I can't figure out how use it.
Note: I know IMEI is not a good way to check for user registration and will be changing it later.
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
// Create instance and populates based on content view ID
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// store IMEI
String imei = tm.getDeviceId();
// store phone
String phone = tm.getLine1Number();
// Display IMEI - Testing Purposes Only
TextView imeiText = (TextView) findViewById(R.id.imeiDisplay);
imeiText.setText("IMEI:" + imei);
// Display phone number - Testing Purposes Only
TextView phoneText = (TextView) findViewById(R.id.phoneDisplay);
phoneText.setText("Phone:" + phone);
new DownloadTask().execute("http://www.url.com/mobileAPI.php?action=retrieve_user_info&IMEI="+imei);
}
private class DownloadTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
try {
return downloadContent(params[0]);
} catch (IOException e) {
return "Unable to retrieve data. URL may be invalid.";
}
}
#Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}
private String downloadContent(String myurl) throws IOException {
InputStream is = null;
int length = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
Log.d(TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = convertInputStreamToString(is, length);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
public String convertInputStreamToString(InputStream stream, int length) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[length];
reader.read(buffer);
return new String(buffer);
}
}
This code returns an xml file, as a toast:
<?xml version="1.0" encoding="ISO-8859-1"?>
<mobile_user_info>
<rec>45</rec>
<IMEI>9900990099009</IMEI>
<fname>First</fname>
<lname>Last</lname>
<instance>instance1</instance>
<registered>N</registered>
</mobile_user_info>
I'm hoping someone can point me in the right direction for separating each line and using it independently. For example, if the Registered line comes back as N, a message is displayed like, 'You are not registered. Please contact administrator.'
Actually you should use an XML parser to parse the server's response. But if responses are always as simple as your example, you can use a regular expression to extract out the IMEI field.
String contentAsString = ...
Pattern pattern = Pattern.compile("<IMEI>(\d*)</IMEI>");
Matcher matcher = pattern.matcher(contentAsString);
if (matcher.find()) {
String imei = matcher.group(1);
}
I want to execute java web service method from android app. My web service method signature looks like:
public String getData(String category) throws Exception
The method returns string and accepts a string as argument.
I have executed the method from google chrome's address bar as:
http://localhost:8080/JsonWebService/services/JsonWebService/getData?category=Marketing
Here getData is the name of the method and Marketing is the argument to that method. From explorer it works fine.
But when I add the same url to android app's httppost request it fails saying wrong number of arguments. My android app code is:
HttpPost post = new HttpPost("http://192.168.1.7:8080/JsonWebService/services/JsonWebService/getData?category=Marketing");
HttpResponse httpres = httpClient.execute(post);
Please Note: Here 192.168.1.7 is required as I am executing the app directly on the device and hence I am not using localhost.
A non argument method executes correctly from android app also.
But when argument is inserted in the url why it fails saying wrong number of arguments in android app and how it executes correctly in google chrome on PC. Please help... Thanks...
I am adding the code here. My java web service code is as follows:
public class JsonWebService {
#POST
#Path("getData")
public String getData(String category) throws Exception {
JSONObject jsonData = new JSONObject();
String Email = "";
String Name = "";
String receivedCat = "";
boolean status = false;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/admindb","root","");
java.sql.PreparedStatement query = con.prepareStatement("SELECT * FROM sample WHERE Category =" + "'" + category + "'" + ";");
ResultSet result = query.executeQuery();
while(result.next()){
receivedCat = result.getString("Category");
Name = result.getString("Name");
Email = result.getString("Email");
}
if(receivedCat.equals(category)){
status = true;
jsonData.put("Name",Name);
jsonData.put("Email", Email);
jsonData.put("status", status);
}
}
catch(Exception e) {
e.printStackTrace();
}
return jsonData.toString();
}
My android client code looks as follows:
btnCategory = (Button)findViewById(R.id.button1);
txtCategory = (EditText)findViewById(R.id.editText1);
gridV = (GridView)findViewById(R.id.gridView1);
txtName = (EditText)findViewById(R.id.editText3);
txtEmail = (EditText)findViewById(R.id.editText2);
btnCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread netThread = new Thread() {
public void run() {
try {
final JSONObject receivedJson;// = new JSONObject();
String URL = "http://192.168.1.7:8080/JsonWebService/services/JsonWebService/getData?category=Marketing?";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
HttpResponse httpres = httpClient.execute(post);
HttpEntity entity = httpres.getEntity();
String json = EntityUtils.toString(entity).toString();
String parts[] = json.split("<ns:return>");
parts = parts[1].split("</ns:return>");
String jsonPart = parts[0];
receivedJson = new JSONObject(jsonPart);
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
txtName.setText(receivedJson.getString("Name"));
txtEmail.setText(receivedJson.getString("Email"));
}
catch(Exception e){
}
}
};
netThread.start();
}
});
}
The problem is in the java client in String URL, which is the string of URL that calls the java web service method. Please help me...