I want to test the following code, but the DDMS always display
"Unknown socket error -1" , I think the code is correct, Must I set up
the emulator or soming? Thank you.

Source Code:

public class SocketApp extends Activity {

    private Button ok;
    private TextView tv1;
    HttpURLConnection uc ;
    URL url;
    private static final String ip="code.google.com/android/images/
logo_android.gif";
    private static final String host="";
    private static final String path = "http://"; + ip +host ;

    ImageView view1;
    InputStream is;
    BufferedInputStream bis;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ok = (Button)findViewById(R.id.Button01);

        ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                openConn();
                sendRequest();
                getRespones();
                closeConn();
            }
        });

    }

    private void openConn(){
        try {
            url=new URL(path);
            uc = (HttpURLConnection)url.openConnection();
            uc.setDoInput(true);
        }catch (MalformedURLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void sendRequest(){
        try {
                Log.i("[SocketApp]"," conn begin .....");
            uc.connect();
            Log.i("[SocketApp]","conn end .....");
        } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }
    }

    private void getRespones(){
        try {
            is = uc.getInputStream();
            Log.d("lenght",""+uc.getContentLength());
            tv1.setText(uc.getContentLength()+"");
            bis = new BufferedInputStream(is);
            Bitmap bm = BitmapFactory.decodeStream(bis);
            view1.setImageBitmap(bm);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    private void closeConn(){
        try {
            uc.disconnect();
            bis.close();
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

  }

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
    package="com.ltinc.cn">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".SocketApp" android:label="@string/
app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>

            </intent-filter>>
        </activity>
    </application>
</manifest>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to