0

Please help me to find a solution:

If I call the URL (below) the server provide me a something like this:

"Name", "Date" "Peter", "01.01.2013" "Paul", "12.12.2012"

So the server provide me an SQL table as text. I tried to request this and convert it in a string. But when I run the app, the string is empty.

Did I encode the URL right?
Did I manage the request right?

Here is the URL:

http://IP/query.html?sql="select * from ADAnreden"

Here is the URL, which is posted by the Log.d:

http://IP/query.html?sql=%22select+*+from+ADAnreden%22

Here is my code:

import java.io.BufferedInputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import android.util.Log;

public class Reader {

    private String result = "leer";
    String strUrl = "getURL: fehlgeschlagen";
    private HttpURLConnection urlConnection;

    public String getStringFromServer(){

        String ip = "XYZ";

        try {
            String query = URLEncoder.encode("\"select * from ADAnreden\"", "utf-8");
            strUrl = "http://"+ip+"/query.html?sql=" + query;
        }  catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d(PACKAGE_NAME, "After getURL"+strUrl);

        Thread thread = new Thread()
        {
            @Override
            public void run() {
                try {
                    URL url = new URL(strUrl);
                    urlConnection = (HttpURLConnection) url.openConnection();
                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                    result = convertStreamToString(in);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    urlConnection.disconnect();
                } 
            }
        };
        thread.start();

        return result;
    }

    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
}

EDIT:

Here is a sample request with the browser (in this case are only rows, so there are no , )

enter image description here

Bolic
  • 705
  • 2
  • 12
  • 30

1 Answers1

0

You should use a standarised api for transfer of data from your webservice, this will save you a lot of time and frustration parsing your own response as there are libraries like GSON ... which can convert JSON objects to JAVA objects for example.

Using asynctask class is appropriate for doing what you are attempting to do. Solution to your problem using the asynctask class is included below with example.

 private void getString() {
                try {

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(url);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                } catch (Exception e) {
                   e.printStackTrace();
                }


                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "UTF-8"));

                    sb = new StringBuilder();

                    String line = null;

                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();

        }
//parse your data here


    }


class getterThread extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progressDialog = new ProgressDialog(Ingolist.this);
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.setMessage("Loading data from server...");
            progressDialog.show();
            progressDialog.setOnCancelListener(new OnCancelListener() {
        @Override
            public void onCancel(DialogInterface arg0) {
            getterThread.this.cancel(true);
        }
           });

        }

        @Override
        protected Void doInBackground(Void... params) {
            getString();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progressDialog.dismiss();

// Update UI here
        }
        }
Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28