dependencies {
// Other dependencies ...
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
package com.example.xujiajia_sx.myexpressotest;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by xujiajia_sx on 2017/8/14.
*/
public class SimpleViewActivity extends Activity{
private TextView tv;
private Button btn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_simple_view);
initView();
}
private void initView() {
tv=findViewById(R.id.tv_simple_view);
btn=findViewById(R.id.btn_simple_view);
tv.setText("111");
btn.setText("222");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setText("777");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_simple_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn_simple_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.example.xujiajia_sx.myexpressotest;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
/**
* Created by xujiajia_sx on 2017/8/14.
*/
public class SimpleViewTest {
@Rule
public ActivityTestRule<SimpleViewActivity> mActivityTestRule =
new ActivityTestRule<SimpleViewActivity>(SimpleViewActivity.class);
@Test
public void textViewTest() throws Exception {
onView(withId(R.id.tv_simple_view))
.check(matches(withText("111")));
}
@Test
public void buttonTest() throws Exception {
onView(withId(R.id.btn_simple_view))
.check(matches(withText("222")))
.perform(click());
onView(withId(R.id.tv_simple_view))
.check(matches(withText("777")));
}
}
package com.example.xujiajia_sx.myexpressotest;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;
/**
* Created by xujiajia_sx on 2017/8/14.
*/
public class ActTestActivity extends Activity{
private TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_act_test);
initView();
}
private void initView() {
tv= findViewById(R.id.tv_act_test);
new Thread(new Runnable() {
@Override
public void run() {
String url =getIntent().getStringExtra("url");
final String s=mHttpClient.getInstance().get(url);
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(s);
}
});
}
}).start();
}
}
package com.example.xujiajia_sx.myexpressotest;
/**
* Created by xujiajia_sx on 2017/8/14.
*/
public class mHttpClient {
private static HttpURLConnectionClient mClient = null;
public static void setClient(HttpURLConnectionClient client) {
mClient = client;
}
public static HttpURLConnectionClient getInstance() {
return mClient;
}
}
package com.example.xujiajia_sx.myexpressotest;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import static android.content.ContentValues.TAG;
/**
* Created by xujiajia_sx on 2017/8/14.
*/
public class HttpURLConnectionClient {
public String get(String url) {
HttpURLConnection conn = null;
try {
URL mURL = new URL(url);
conn = (HttpURLConnection) mURL.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(2000);
conn.connect();
InputStream is = conn.getInputStream();
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
} catch (IOException e) {
Log.e(TAG, "network error for mini program ", e);
return "";
} finally {
//最后将conn断开连接
if (conn != null) {
conn.disconnect();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_act_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.example.xujiajia_sx.myexpressotest;
import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
/**
* Created by xujiajia_sx on 2017/8/14.
*/
public class ActTest {
@Rule
public ActivityTestRule<ActTestActivity> mActivityTestRule=
new ActivityTestRule<ActTestActivity>(ActTestActivity.class){
@Override
protected Intent getActivityIntent() {
Intent intent=new Intent();
intent.putExtra("url","http://www.weather.com.cn/adat/sk/101310201.html");
return intent;
}
@Override
protected void beforeActivityLaunched() {
mHttpClient.setClient(new HttpURLConnectionClient());
}
};
@Test
public void mTest() throws Exception{
Thread.sleep(5000);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有