0
votes

I'm using the following code

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://domain.com/~path/page.php");
try 
    {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("param", value1));
        nameValuePairs.add(new BasicNameValuePair("param2", value2));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } 
    catch (Exception e)
    {
        e.printStackTrace();
    }

I believe the problem problem is that "~" on a folder in the path. I can't figure out what encode I must use to make the URL valid. I had issues before with "ã" and I had to use a certain encoding that turned the url into "http://XN--SEULEITO-XZA.COM/" , but this encoding doesn't work for isolated "~"'s. This is what the printstack outputs.

10-21 11:40:02.311  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ android.os.NetworkOnMainThreadException
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-21 11:40:02.321  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at com.veustechnology.titansaude.LoginActivity.onClick(LoginActivity.java:194)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.view.View.performClick(View.java:4377)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.view.View$PerformClick.run(View.java:18044)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5306)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-21 11:40:02.331  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-21 11:40:02.341  19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
3
Isn't the tilde reserved for the root folder only? Maybe security blocks it because of that.G_V

3 Answers

0
votes

Encode your URL String in utf-8

String query = URLEncoder.encode(""http://domain.com/~path/page.php"", "utf-8");
HttpPost httppost = new HttpPost(query);
0
votes

The reason you're getting a NetworkOnMainThreadException is that you're trying to lock the main thread by executing a HttpRequest. Don't do that!

Use an AsyncTask instead.

class GetResponse extends AsyncTask<Void, Void, HttpResponse> {

    private Exception exception;

    protected HttpResponse doInBackground(Void... params) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://domain.com/~path/page.php");
        try
        {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("param", value1));
            nameValuePairs.add(new BasicNameValuePair("param2", value2));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            return response;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    protected void onPostExecute(HttpResponse response) {

    }
}
0
votes

Nevermind, the airhead here turned off the device's wifi last friday and forgot about it.