2.3.3 to 4.2.2 Http Get - Help me conquer learning curve

  • Replies:5
Gerard Beaulieu
  • Forum posts: 3

Feb 19, 2014, 7:06:46 AM via Website

Hey all!

New to Android but not developing. App runs great with emulator running Android 2.3.3. When I install the app to my phone (Samsung Galaxy 4S Active) runnnig Android 4.2.2, the Http Get execute command on the client does not seem to happen. I get no data back reported to my TextView.

I know there is some learning curve I'm missing here, and would appreciate some guidance.


HttpExample.java
1package com.example.test;
2
3import android.app.Activity;
4import android.os.Bundle;
5import android.widget.TextView;
6
7public class HttpExample extends Activity {
8
9 TextView httpStuff;
10
11 @Override
12 protected void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.httpex);
15 httpStuff = (TextView) findViewById(R.id.tvHttp);
16 GetMethodEx test = new GetMethodEx();
17 try {
18 String returned = test.getInternetData();
19 httpStuff.setText(returned);
20 } catch (Exception e) {
21 // TODO Auto-generated catch block
22 e.printStackTrace();
23 }
24
25
26 }
27
28
29}

GetMethodEx.java
1package com.example.test;
2
3import java.io.BufferedReader;
4import java.io.InputStreamReader;
5import java.net.URI;
6
7import org.apache.http.HttpResponse;
8import org.apache.http.client.HttpClient;
9import org.apache.http.client.methods.HttpGet;
10import org.apache.http.impl.client.DefaultHttpClient;
11
12public class GetMethodEx {
13
14 public String getInternetData () throws Exception {
15 BufferedReader in = null;
16 String data = null;
17 try {
18 HttpClient client = new DefaultHttpClient();
19 URI website = new URI("CANTPOSTURL");
20 HttpGet request = new HttpGet();
21 request.setURI(website);
22 HttpResponse response = client.execute(request);
23 in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
24 StringBuffer sb = new StringBuffer("");
25 String l = "";
26 String nl = System.getProperty("line.separator");
27 while ((l = in.readLine()) !=null) {
28 sb.append(l + nl);
29
30 }
31 in.close();
32 data = sb.toString();
33 return data;
34
35 }finally {
36 if (in !=null) {
37 try {
38 in.close();
39 return data;
40 }catch (Exception e) {
41 e.printStackTrace();
42 }
43 }
44 }
45
46
47 }
48
49}

AndroidManifest.xml
1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="CANTPOSTURL"
3 package="com.example.test"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="18" />
10
11 <uses-permission android:name="android.permission.INTERNET" />
12 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
13
14 <application
15 android:allowBackup="true"
16 android:icon="@drawable/ic_launcher"
17 android:label="@string/app_name"
18 android:theme="@style/AppTheme" >
19 <activity
20 android:name="com.example.test.HttpExample"
21 android:label="@string/app_name" >
22 <intent-filter>
23 <action android:name="android.intent.action.MAIN" />
24
25 <category android:name="android.intent.category.LAUNCHER" />
26 </intent-filter>
27 </activity>
28 </application>
29
30</manifest>

httpex.xml
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="CANTPOSTURL"
3 xmlns:tools="CANTPOSTURL"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical"
7 tools:context=".HttpActivity">
8
9 <ScrollView android:layout_height="fill_parent" android:layout_width="fill_parent">
10
11 <TextView
12 android:id="@+id/tvHttp"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:text="Loading Data" >
16 </TextView>
17 </ScrollView>
18
19</LinearLayout>

I would post the logcat but there really is no issue at 2.3.3... just need to know what i'm missing going to 4.2.2.

Thanks in advance, I know this should be easy for you all.

Reply
Pascal P.
  • Admin
  • Forum posts: 11,286

Feb 19, 2014, 5:24:26 PM via Website

Hello, here you have a Network on mainException.
Tis means you can't execute http get in androids main thread. Since Android 4.0 all network requests should be performed in separate thread or exception will occurs. For solving this take a look on the AsyncTask on d.android.com.

— modified on Feb 19, 2014, 5:24:46 PM

LG Pascal //It's not a bug, it's a feature. :) ;)

Gerard Beaulieu

Reply
Gerard Beaulieu
  • Forum posts: 3

Feb 19, 2014, 9:07:37 PM via Website

a fine response thanks! At least I know what the problem is now.

any example code anywhere???

Reply
Pascal P.
  • Admin
  • Forum posts: 11,286

Feb 19, 2014, 9:23:40 PM via Website

For example Code look here:http://mobiledevtuts.com/android/android-http-with-asynctask-example/
But when i can google this i think you can google too :)

LG Pascal //It's not a bug, it's a feature. :) ;)

Gerard Beaulieu

Reply
Gerard Beaulieu
  • Forum posts: 3

Feb 19, 2014, 10:45:54 PM via Website

thx Pascal... I know, I google'd it as well. I was more asking for some examples you or others like...

I know how to use the java.util.concurrent package using the ThreadPools and Executor classes, but I know that Android provides android.os.Handler class or the AsyncTasks classes. So I want to make sure I'm coding to convention.

thx again for the response!!!

Reply
Pascal P.
  • Admin
  • Forum posts: 11,286

Feb 20, 2014, 7:33:17 AM via Website

Now i dont know what kind of example you need.
Some code with the async task or normal android threading?
When you google something like http get threading android
You shall get your examples

LG Pascal //It's not a bug, it's a feature. :) ;)

Reply