Problem with openConnection?

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Problem with openConnection?

Postby ramgraph1 » Tue Feb 12, 2008 10:16 am

I am using the code below based on what was taught in "Getting Data From the Web (URLConnection via http)".
I want Android to open a URL that adds data to a php file. The URL, when opened in a web browser, works correctly with the php file and updates my database as desired but the same URL in Android does nothing. I have the TextView to check the URL and the Exception stays null. It seems to me like this should work (though that could easily be due to my ignorance!).
Could someone please tell me why this is not working and if possible how to fix it? Thanks.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class myphptest extends Activity {
  2.  
  3.  
  4.  
  5. @Override
  6.  
  7. public void onCreate(Bundle icicle) {
  8.  
  9.      super.onCreate(icicle);
  10.  
  11.     TextView print = new TextView(this);
  12.  
  13.  
  14.  
  15.         String compURL = ("url.php?name=etc");
  16.  
  17.         String myString = null;
  18.  
  19.         try {
  20.  
  21.         URL postScoreURL = new URL(compURL);
  22.  
  23.         postScoreURL.openConnection();
  24.  
  25.         } catch (Exception e) {
  26.  
  27.                 myString = e.getMessage();
  28.  
  29.         }
  30.  
  31.        
  32.  
  33.         print.setText(compURL + " Exception = " + myString);
  34.  
  35.         this.setContentView(print);
  36.  
  37.  }
  38.  
  39.  }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Postby plusminus » Tue Feb 12, 2008 10:25 am

Hello ramgraph,

aren't you missing the domain-name or ip :?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String compURL = ("url.php?name=etc");
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Tue Feb 12, 2008 1:39 pm

Thanks for responding. The URL I am using in the code works perfectly when I put in in a web browser (it updates my database with the info after the "?" - name, title, etc.) - even in the web browser in the Android emulator. When used from the app I have with the code above, it does nothing (no change in the database). So the issue is not the URL as far as I can tell, but is instead with the connection going through in the browser but not in the Android app...
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby ramgraph1 » Tue Feb 12, 2008 2:33 pm

Just to be clear, the "url.php?name=etc" is of course not the URL I am using. Perhaps I should have typed "http://mydomain.com/myphp.php?name=etc." or something like that...
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby plusminus » Tue Feb 12, 2008 2:35 pm

Hello ramgraph,

I'm still pretty sure, that this: " url.php?name=etc " is not a valid url! That url should not even be treated as from localhost.

Did you check the returnvalue of openConnection, like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. URLConnection uc = postScoreURL.openConnection();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

if it is null or sth. :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Tue Feb 12, 2008 2:57 pm

Sorry I was slow about clearing the URL issue up (the post I made a few minutes before yours). My bad. At any rate I tested the actual URL (http://www.mydomaingoeshere.com/myphp.p ... e=newtitle and so on) in both Safari and Opera and also in the Android browser on the emulator and it does work properly with the php file and mySQL database on my webhost's sever.
Again sorry for the confusion, but does it look like there's anything wrong with the rest of the code?
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Postby ramgraph1 » Tue Feb 12, 2008 3:00 pm

And in response to your question, I tried various things including these:

Code: Select all
HttpURLConnection http = (HttpURLConnection)postScoreURL.openConnection();
http.setRequestMethod("GET");
http.setDoOutput(true);
http.connect();


but nothing has gotten it to work...
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby plusminus » Tue Feb 12, 2008 4:30 pm

Hello ramgraph,

:arrow: :src: this topic/post will probably help you further. IT contains code for HTTP Post and HTTP Get.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Tue Feb 12, 2008 10:46 pm

Thanks for responding again.
Actually, that topic was what got me started trying to use php with my app. But it uses queueRequest which complicates things quite a bit and though I have tried to implement it a couple of times have only met with failure (I posted a problem to that topic the other day as well).

I knew from studying php that a database could be updated by simply editing the URL after the php file's location and thought the code below would be a simple solution I could understand. Really, if the URL being opened in the Android web browser updates the database, it certainly seems the same URL opened in another Android app should do the same. So I don't understand why things aren't happening here the way they seem to me that they ought to. The only thing I can think of is that openConnection isn't really opening a connection even though there is no exception being thrown...
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby plusminus » Tue Feb 12, 2008 10:53 pm

Hello rampgraph,

you only need to use the public functions :darrow: , like Nanodeath said. So it is pretty easy.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Map<String,String> vars = new HashMap<String,String>();
  2.  
  3.                vars.put("test", "value");
  4.  
  5.                vars.put("othertest", "valuedos");
  6.  
  7.                
  8.  
  9.                HTTP_Request request = new HTTP_Request(NutriScan.this, "http://www.samplewebsite.com/test.php", HTTP_Request.Method.GET, vars);
  10.  
  11.                request.setHandler(new PostHandler(){
  12.  
  13.                     void run(String response) {
  14.  
  15.                          Log.d(TAG, "RESPONSE: " + response);
  16.  
  17.                     }
  18.  
  19.  
  20.  
  21.                     @Override
  22.  
  23.                     void error(String error) {
  24.  
  25.                          Log.e(TAG, "ERROR: " + error);
  26.  
  27.                     }
  28.  
  29.  
  30.  
  31.                     @Override
  32.  
  33.                     void status(String status) {
  34.  
  35.                          Log.i(TAG, "STATUS:" + status);
  36.  
  37.                     }
  38.  
  39.                });
  40.  
  41.                request.go();
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Tue Feb 12, 2008 11:23 pm

OK. I will go back and dig into those some more as I am sure it will be useful to know.
Actually I was able to fix the code I posted above by changing to this:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 // ...
  2.  
  3.                 uploadInfo(compURL);
  4.  
  5.         }
  6.  
  7.         // ...
  8.  
  9.         void uploadInfo(String phpAdd){
  10.  
  11.                 URL myphpAdd =null;
  12.  
  13.                 try {
  14.  
  15.                         myphpAdd= new URL(phpAdd);
  16.  
  17.                 } catch (MalformedURLException e) {
  18.  
  19.                         e.printStackTrace();
  20.  
  21.                 }
  22.  
  23.                 try {
  24.  
  25.                         HttpURLConnection conn= (HttpURLConnection)myphpAdd.openConnection();
  26.  
  27.                         conn.setDoInput(true);
  28.  
  29.                         conn.connect();
  30.  
  31.                         InputStream is = conn.getInputStream();
  32.  
  33.                 } catch (IOException e) {
  34.  
  35.                         e.printStackTrace();
  36.  
  37.                 }
  38.  
  39.                 // ...
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


I don't know why it works though! The "InputStream is = conn.getInputStream();" seems to be essential...
Thanks for all of the time you put into helping me with this. Without the tutorials I found here I wouldn't have had a clue how to start!
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google [Bot] and 3 guests