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

Recommended Answers

All 4 Replies

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

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

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

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.:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.