小编Raj*_*tth的帖子

如何在测试期间调用oncreate之前获取活动引用

如何在调用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 mocking android-testing junit-rule android-espresso

9
推荐指数
2
解决办法
6254
查看次数

如何用Volley中的数据发送帖子请求?

这是我的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并且它有效地工作有什么办法可以用凌空来做到这一点吗?

android android-networking android-volley

2
推荐指数
1
解决办法
2万
查看次数