标签: android-networking

从 android 模拟器 Ping 主机

我正在尝试从 android 模拟器访问托管在我的 dev Ubuntu 机器上的网页。我可以从模拟器浏览互联网,但无法访问主机开发机器的网页,导致 tcp_error。我也无法 ping 主机。我尝试使用主机的 IP 地址而不是 localhost 或 127.0.0.1。

任何的想法?

android android-emulator android-networking

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

如何用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万
查看次数

如何在 Android XML 布局文件中放置按钮,就像在网格视图中一样

我正在尝试在网格视图中放置 12 个按钮。这是我的布局XML文件。我该如何使用RelativeLayout来实现这一目标?我是 Android 编程新手。

 <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
     <LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button
        android:id="@+id/bAries"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aries"
        android:drawableTop="@drawable/aries" />

    <Button
        android:id="@+id/bTauras"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tauras"
        android:drawableTop="@drawable/tauras" />

    <Button
        android:id="@+id/bGemini"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gemini"
        android:drawableTop="@drawable/gemini" />
Run Code Online (Sandbox Code Playgroud)

android android-networking android-layout

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

实现 Retrofit2 服务接口

想象一下下面的场景。

我使用 Retrofit 作为我的 API 客户端,我将使用缓存机制在本地保存一些响应,比如 SQLite 数据库,所以一些数据只获得一次。为以下接口创建自定义实现似乎是一个完美的案例,该接口将在网络不可用时从本地数据库获取数据。

public interface CommentsService {  
    @GET("posts/{postId}/comments")
    Call<List<Comment>> getCommentsOfPost(@Path("postId") int id);
}
Run Code Online (Sandbox Code Playgroud)

问题在于,retrofit2 中的所有服务方法都应该包装在一个Call对象中,因为它会阻止像以前版本的改造一样将回调作为参数传递。

我想知道创建此接口的自定义实现是否是一个好习惯,如果是,那么如何处理 Call 对象?

android android-networking retrofit retrofit2

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

bindProcessToNetwork 不适用于 android 中的 ffmpeg

我正在使用FFmpeg将视频从相机(通过 wifi 连接而没有互联网连接)重新流式传输到另一台服务器,我想通过蜂窝数据进行重新流式传输过程。因为我已经连接到 wifi 并同时使用蜂窝数据 bindProcessToNetwork()。在执行 ffmpeg 命令之前,我已完成以下操作

final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder req = new NetworkRequest.Builder();
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {
    @Override
    public void onAvailable(Network network) {
        //here you can use bindProcessToNetwork
        //cm.getNetworkInfo(network);
        if (cm.getNetworkInfo(network).getType() == ConnectivityManager.TYPE_MOBILE) {
            cm.bindProcessToNetwork(network);
        }
    }

});
Run Code Online (Sandbox Code Playgroud)

在大多数情况下它工作正常,就像 webview 通过使用蜂窝数据正常工作,同时连接到 wifi,但是当我尝试执行任何 ffmpeg 命令时它不起作用。

android ffmpeg android-networking android-connectivitymanager

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

RecyclerView displaying wrong Images

The following is the code for the adapter I'm using to display movie items which contain Movie Image and Movie Title.

   public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {

 private List<Movie> movieList;



public MovieAdapter(List<Movie> movieList){
   this.movieList = movieList;
}

@Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.movie_item,parent,false);

    return new MovieViewHolder(view);

}

@Override
public void onBindViewHolder(final MovieViewHolder holder, int position) {

   Movie movie = movieList.get(position);

   holder.mv_name.setText(movie.getName());



    class ImageDownloadTask extends AsyncTask<String,Void,Bitmap>{
       @Override …
Run Code Online (Sandbox Code Playgroud)

android android-networking android-recyclerview

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

可以观察网络状态吗?

我想知道是否有办法以某种方式注册为网络访问的观察者(并且当互联网可用时运行一些回调函数),或者如果轮询是唯一的方法.谢谢!

android android-networking

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

从JSON url读取数据但android不允许.NetworkOnMainThread异常和StrictMode $ AndroidBlockGuardPolicy错误

我正在尝试在Android应用程序中使用Google Map.MainActivity包含EditText,ImageButton和MapView.当用户提供一些查询并点击ImageButton时,MainAcitivity应该从url获取一些JSON数据然后我需要使用该JSON数据在用户单击这些Markers时创建Markers和dialogBox.每当我运行应用程序时,它运行正常.但是当我单击ImageButton时,应用程序停止给出logcat错误NetworkOnMainThreadException和StrickMode $ AndroidBlockGuardPolicy.我附加了logcat输出和MainActivity.我运行这个JSON程序创建其他测试项目,它工作得很好.但是当我将它添加到MainAcitivity时,它不起作用.我相信,android的安全策略阻止它从url获取数据.

那么这个错误的原因是什么?如何使这个Activity工作?非常感谢朋友们的关注.如果您需要更多CustomOverlayItem对象或JSON对象的代码,请告诉我.

public class MainActivity extends MapActivity {

        private List<Home> listHome = new ArrayList<Home>();

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            MapView mv_map = (MapView) findViewById(R.id.mv_map);
            MapController mapController = mv_map.getController();
            mv_map.setBuiltInZoomControls(true);
            mapController.setZoom(5);
            Markers markers = new Markers(this.getResources().getDrawable(
                    R.drawable.ic_action_home), this);
            CustomOverlayItem items = new CustomOverlayItem(getuserLocation(),
                    "Checking", "Checking");
            List<Overlay> overlays = mv_map.getOverlays();
            markers.addOverlayItem(items);
            overlays.add(markers);

            ImageButton ib_search = (ImageButton) findViewById(R.id.ib_search);
            ib_search.setOnClickListener(new OnClickListener() {

            @Override
                public void onClick(View v) {
                    Json json = new Json();
                    listHome = json.getListHome();
                    EditText editText …
Run Code Online (Sandbox Code Playgroud)

android json gson android-networking android-mapview

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

使用HttpEntity的consumeContent()

Android 中consumeContent()的类或org.apache.http.HttpEntity的目的是什么?

什么时候应该使用它可以有副作用吗?

我正在尝试修复应用程序中的错误,该应用程序使用HttpClient向服务器发出请求,有时如果某个特定请求失败,它将随后失败,尽管互联网是正常的.应用程序在输入流读取结束时调用此方法.

java android httpclient android-networking

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

检查网络连接android

我是ServiceAndroid的新用户,我很困惑.我有和Activity我想检查网络连接的所有时间(wifi或3g).我实现了一个简单的Service和一个BroadcastReveiver.

这里的代码 Service

public class NetworkCheck extends IntentService {

private final String CONNECTION = "connection";

public NetworkCheck(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent workIntent) {

    ConnectivityManager conMgr = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);


    String dataString = workIntent.getDataString();

    if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
            || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {

        // notify user you are not online
        Intent localIntent = new Intent().putExtra(CONNECTION, 0);

        LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);

    }
}
Run Code Online (Sandbox Code Playgroud)

这里的代码 BroadcastReceiver

public class NetworkReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context …
Run Code Online (Sandbox Code Playgroud)

service android broadcastreceiver android-networking intentservice

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