-1

I am developing an app which includes the upload of all contacts to my server what i am doing is displaying all the contacts and sending them to my server as a string.Here is my code:

package com.example.core.freemusic;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class MainActivity extends Activity {
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ll = (LinearLayout) findViewById(R.id.LinearLayout1);
    LoadContactsAyscn lca = new LoadContactsAyscn();
    lca.execute();
}
class LoadContactsAyscn extends AsyncTask < Void, Void, String > {
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(MainActivity.this, "Please Wait","");
    }
    @Override
     protected String doInBackground(Void...params) {
        String contacts = "";
        Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (c.moveToNext()) {
            String contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contacts += contactName + ":" + phNumber + "\n";
        }
        c.close();
        return contacts;
    }
    @Override
     protected void onPostExecute(String contacts) {
        super.onPostExecute(contacts);
        pd.cancel();
        TextView a = (TextView) findViewById(R.id.textView);
        a.setText(contacts);
        doInBackground(contacts);
    }
    protected void doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.2.30/b.php?username="+params);
        try {
            httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

Actually the code shows no error but on running it in emulator it crashes. the error on logcat is:

05-24 14:00:57.989    2488-2500/com.example.core.freemusic W/art﹕    Suspending all threads took: 5.841ms
05-24 14:00:57.992    2488-2500/com.example.core.freemusic I/art﹕ Background partial concurrent mark sweep GC freed 1888(115KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 502KB/1014KB, paused 6.923ms total 95.188ms
05-24 14:00:58.073    2488-2488/com.example.core.freemusic I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
05-24 14:00:58.080    2488-2488/com.example.core.freemusic D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
05-24 14:00:58.102    2488-2488/com.example.core.freemusic D/AndroidRuntime﹕ Shutting down VM
05-24 14:00:58.103    2488-2488/com.example.core.freemusic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.core.freemusic, PID: 2488
java.lang.IllegalArgumentException: Illegal character in query at index 43: http://192.168.2.30/b.php?username=Sd:(546) 456-4826
Sad:486-44
        at java.net.URI.create(URI.java:730)
        at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
        at com.example.core.freemusic.MainActivity$LoadContactsAyscn.onPostExecute(MainActivity.java:64)
        at com.example.core.freemusic.MainActivity$LoadContactsAyscn.onPostExecute(MainActivity.java:38)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-24 14:01:01.259    2488-2488/com.example.core.freemusic I/Process﹕ Sending signal. PID: 2488 SIG: 9
05-24 15:24:12.790    2685-2685/com.example.core.freemusic D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
3
  • 1
    can you show the error? Commented May 24, 2015 at 10:20
  • I edited the question with errors. Commented May 24, 2015 at 10:25
  • 1
    your error log clearly says java.lang.IllegalArgumentException: Illegal character in query at index 43: http://192.168.2.30/b.php?username=Sd:(546) 456-4826 please start debugging from here Commented May 24, 2015 at 10:26

1 Answer 1

0

As error code state, you need to encode the query:

HttpPost httppost = new HttpPost("http://192.168.2.30/b.php?username="+params);

need to changed into:

HttpPost httppost = new HttpPost("http://192.168.2.30/b.php?username="+java.net.URLEncoder.encode(params[0], "UTF-8"));
Sign up to request clarification or add additional context in comments.

2 Comments

Now its showing error that String[] cannot be converted to String
Check the updated code. Btw, based on that error, you should understand that it is because params is an array of String (check the [] part). URLEncoder.encode() require String (not array of String).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.