0

I am a newbie to Android Studio and have been under this problem for a few days. My code is simple, which requests the information of the website that I specify when I push a button and get the response from that website. The problem is that my android app can not connect to the website and get any data. There is no error during the build processing but when I start my app and push a button(data from the website should be on the screen), nothing appears on the screen.

Here are the things that I tried to fix this problem. 1. Recreate AVD (it didn't work). 2. Remove some of image files because my error message was "skipped frames the application may be doing too much work on its main thread".

Here is my code below. I think it does not seem to have any problems in the below code because I coded the below code, following the lectures on youtube. So I guess this issue might be on configure / setting mode. Anyone know how to fix this..?

Thanks in advance.

  • Manifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.lhc.test_1">
    
        <uses-permission android:name="android.permission.INTERNET"/>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
  • MainActivity.java

    package com.example.lhc.test_1;
    
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class MainActivity extends AppCompatActivity {
    
        public static final String TAG = "MainActivity";
        Handler handler;
        TextView text01;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            text01 = (TextView)findViewById(R.id.text01);
    
            final Button requestButton = (Button)findViewById(R.id.requestButton);
            requestButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    request();
                }
            });
    
            handler = new Handler();
        }
    
        private void request() {
    
            try {
                URL url = new URL("http://m.google..com"); // 페이지 정보 저장
                HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 연결
    
                if(conn != null) {
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setConnectTimeout(10000);
    
                    int resCode = conn.getResponseCode(); // 연결 요청 및 정보 요청
                    if(resCode == HttpURLConnection.HTTP_OK) {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
                        while(true) {
                            String aLine = reader.readLine();
                            if(aLine == null) {
                                break;
                            }
    
                            appendText(aLine);
                        }
                        reader.close();
                    }
                    conn.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void appendText(String msg) {
            final String inMsg = msg;
            handler.post(new Runnable() {
                @Override
                public void run() {
                    text01.append(inMsg + "\n");
                }
            });
        }
    }
    
  • activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/requestButton"
            android:layout_margin="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="요청"
            />
    
        <TextView
            android:id="@+id/text01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ttt"
            android:background="#ffffff"
            android:textSize="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </LinearLayout>
    
MinYoung Lee
  • 51
  • 1
  • 9
  • I suggest you look at using something like Volley or OkHttp libraries https://stackoverflow.com/questions/16902716/comparison-of-android-networking-libraries-okhttp-retrofit-and-volley – OneCricketeer Jul 04 '18 at 17:46
  • cricket_007 I am sorry for the late response, just checked this now. I fixed that issue by removing AVD and reinstalling it. Thanks for your answer tho. – MinYoung Lee Jul 13 '18 at 08:43

0 Answers0