Chevy Mark Sunderland

Chevy Mark Sunderland

  • NA
  • 188
  • 166.3k

How to add values in spinner in android form with array

Mar 10 2014 5:33 AM
How to add values in spinner in android form with array connected to text file in remote host ? This code print FATAL EXCEPTION: main .... can you help me? Any help would be appreciated .............

03-09 23:29:23.321: E/AndroidRuntime(3204): FATAL EXCEPTION: main
03-09 23:29:23.321: E/AndroidRuntime(3204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx.xxxxx/com.xxxxx.xxxxx.SpinnerPopText}: android.os.NetworkOnMainThreadException
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.access$700(ActivityThread.java:140)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.os.Looper.loop(Looper.java:137)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.main(ActivityThread.java:4935)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 23:29:23.321: E/AndroidRuntime(3204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-09 23:29:23.321: E/AndroidRuntime(3204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-09 23:29:23.321: E/AndroidRuntime(3204): at dalvik.system.NativeStart.main(Native Method)
03-09 23:29:23.321: E/AndroidRuntime(3204): Caused by: android.os.NetworkOnMainThreadException
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
03-09 23:29:23.321: E/AndroidRuntime(3204): at com.xxxxx.xxxxx.SpinnerPopText.onCreate(SpinnerPopText.java:37)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.Activity.performCreate(Activity.java:5206)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
03-09 23:29:23.321: E/AndroidRuntime(3204): ... 11 more


java class

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.app.Activity;

public class SpinnerPopText extends Activity {

Spinner sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerpoptext);

String path = "http://www.myhost.com/public/country.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u
.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer);
bo.write(buffer);


final List<String> list=new ArrayList<String>();
list.add(bo.toString());


final Spinner sp=(Spinner) findViewById(R.id.my_spinner);
ArrayAdapter<String> adp= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adp);
(android.R.layout.simple_spinner_dropdown_item);
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}