<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rock.myhandlertest1">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
2. gradle:(Module app) 에 AsyncHttpClient 추가
compile 'com.loopj.android:android-async-http:1.4.9'
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rock.myhandlertest1.MainActivity"
android:focusableInTouchMode="false">
<EditText
android:layout_width="250sp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="http://m.naver.com"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:id="@+id/button"
android:layout_alignTop="@+id/editText"
android:layout_alignParentEnd="true" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top" />
</ScrollView>
</RelativeLayout>
4. MainActivity.java
package com.example.rock.myhandlertest1;
import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static TextView tvUrl;
static TextView tvContent;
String mStrUrl = "";
Handler mHandler;
MyRunnable myRunnable;
InputMethodManager imm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
tvUrl = (TextView) findViewById(R.id.editText);
tvContent = (TextView) findViewById(R.id.content);
mHandler = new Handler();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvUrl.clearFocus();
tvContent.setText("");
// 키보드 화면에서 내리기
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tvUrl.getWindowToken(), 0);
mStrUrl = tvUrl.getText().toString();
myRunnable = new MyRunnable(mStrUrl);
mHandler.post(myRunnable);
}
});
}
}
5. MyRunnable.java
package com.example.rock.myhandlertest1;
import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import cz.msebera.android.httpclient.Header;
/**
* Created by rock on 2016-05-13.
*/
public class MyRunnable implements Runnable {
AsyncHttpClient httpClient;
final String url;
final String TAG = "MYTEST";
public MyRunnable(final String pUrl) {
url = pUrl;
}
@Override
public void run() {
httpClient = new AsyncHttpClient();
httpClient.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d(TAG,"Http get Success");
String text = new String(responseBody);
MainActivity.tvContent.setText(text);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d(TAG,"Http get Fail");
Log.d(TAG,"Status Code:"+statusCode);
Log.d(TAG,"Error:"+error.getMessage());
MainActivity.tvContent.setText("Http get fail.\nstatus code="+statusCode+"\nError:"+error.getMessage());
}
});
}
}
6. 실행결과
댓글 없음 :
댓글 쓰기