如何在调用onCreate之前获取Activity的引用.而它的测试.我使用ActivityTestRule作为JUnit规则.这个要求的原因是我想从测试中将Mocks注入活动.
public class MyActivity extends Activity{
MyComponent myComponent;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(myComponent==null){
myComponent ... //initialise dagger component
}
myComponent.inject(this);
...
}
public void setComponent(MyComponent comp){
this.myComponent = comp;
}
}
public class MyTest{
@Rule
public ActivityTestRule<MyActivity> intentsTestRule = new ActivityTestRule<>(MyActivity.class);
MyComponent myFakeComponent;
@Before
public void setUp() {
MyActivity activity = intentsTestRule.getActivity();
activity.setComponent(myFakeComponent);
}
@Test
public void testMethod1(){...}
}
Run Code Online (Sandbox Code Playgroud) 这是我的android代码
JSONObject params = new JSONObject();
params.put("username","rajesh@gmail.com");
params.put("password","hellothere");
JsonObjectRequest loginRequest = new JsonObjectRequest(
Request.Method.POST,
"http://192.168.2.67/tmp/test.php",
params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Log.d("","");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("","");
}
});
requestQueue.add(loginRequest);
Run Code Online (Sandbox Code Playgroud)
php中的服务器代码
<?php
$usr = $_REQUEST['username'];
$pwd = $_REQUEST['password'];
$resp[$usr]=$pwd;
echo json_encode($resp);
?>
Run Code Online (Sandbox Code Playgroud)
我得到的回答是{"":null}
我尝试使用apache http cleint并且它有效地工作有什么办法可以用凌空来做到这一点吗?