954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HTTP request problem

Hi all :)
I got some serius problem that I've tried to solve in almost two days.
I've try to send a request to a URL from a android app, http://web.hjalmar.nu/~morgan/webpage/test.php . the problem is, that it isn't working.
ANDROID-file

package morgan.request.test;





import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;


public class TestingrequestActivity extends Activity {
    /** Called when the activity is first created. */
    
	Button post;
    EditText txt;
    TextView outputt;
	
    public String postData() {
    	String result = "";
    	try {
			URL url = new URL("http://web.hjalmar.nu/~morgan/webpage/test.php");
			
			URLConnection urlConn = url.openConnection();
			urlConn.setDoInput(true);
			urlConn.setDoOutput(true);
			urlConn.addRequestProperty("Http.host", "web.hjalmar.nu");
			urlConn.addRequestProperty("Http.port", "80");
			urlConn.setUseCaches(false);
			
			
			DataOutputStream output = new DataOutputStream(urlConn.getOutputStream());
			String content = "name=morgan";
			output.writeBytes(content);
			output.flush();
			output.close();
			
			DataInputStream in = new DataInputStream(urlConn.getInputStream());
			BufferedReader input = new BufferedReader(new InputStreamReader(in));
			String str;
			while ((str = input.readLine()) != null) {
				result = result + str + "\n";
			}
			input.close();
		}
		catch (MalformedURLException e) {
			result = e + "\n";
		}
		catch (IOException e) {
			result = e + "\n";
		}
    	return result;
    }
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        outputt = new TextView(this);
        outputt.setText(postData());
        outputt.setHeight(10);
        setContentView(outputt);
        
        
        
    }
}

php-file:

<?php
echo "hello" . £_POST["name"];
?>


I get the error

java.net.UnknownHostException: web.hjalmar.nu

but the url is correct. you can try to click to the link to the php file.
I have taked the code from a tutorial site, so it chould work, but it don't.
I've quested a android programmer I know abaut the error, but he couldn't help
me.

can someone help me plz? you guys up there on daniweb is my only hope :S

emorjon2
Newbie Poster
23 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

Have you set <uses-permission android:name="android.permission.INTERNET" /> in the AndroidManifest.xml file?

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 
Have you set <uses-permission android:name="android.permission.INTERNET" /> in the AndroidManifest.xml file?


Yes I have. still it's not working :(

emorjon2
Newbie Poster
23 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

Well please take a look at this blog post - Tips to solve the UnknownHostException on Android

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 
Well please take a look at this blog post - Tips to solve the UnknownHostException on Android


thank you for the site, but I CAN connect to google throught the webbrownser so it can't be any wrong with the emulator, it must be somewhere in the code, but I can't see any wrong in the code. I have test a applet now to connect to the php-file and then
it work, but it don't work on the emulator.:-/

emorjon2
Newbie Poster
23 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: