基于android studio真机连接本地服务器(Apache)详细流程

第一步:肯定是下载好Apache,这里就不说了,其他博客也讲了配置;

第二步:打开服务器,保证手机电脑连接同一个路由器,就是官方语言局域网;

第三步:关闭电脑防火墙

看到防火墙了吧,三个网络防火墙都要关闭

第四步:修改url的IP地址,默认是

修改为:

具体有什么不同呢:

1.首先

192.168.2.161

这个是你电脑IP地址,通过cmd输入ipconfig


选择这里的ipv4地址

2.:88是你下载服务器的时候,自己修改配置时候(.conf)修改值


你自己修改的端口值

3.就是你自己写的xml或者json格式文件名了

完成以上内容,就可以在真机上用浏览器或者你写的解析程序访问本地服务器了

加油!!!!

有问题随时联系我

发布了2 篇原创文章 · 获赞 3 · 访问量 1943
展开阅读全文

android studio 与后台交互问题 网络访问的问题

02-25

url返回了我需要的值 只有我在控制台点击url的时候 服务器端才会有反应 HttpResponse response = httpClient.execute(httpGet);不执行 代码如下: package com.cy.mynj.com.cy.mynj.login; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.cy.mynj.R; import com.cy.mynj.com.cy.mynj.activities.MainActivity; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; public class LoginActivity extends AppCompatActivity implements View.OnClickListener{ private EditText et_phone; private EditText et_password; private Button btn_login; private TextView tv_login_register; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initViews(); } private void initViews() { et_phone=(EditText)findViewById(R.id.et_phone); et_password=(EditText)findViewById(R.id.et_password); btn_login=(Button)findViewById(R.id.btn_login); btn_login.setOnClickListener(this); tv_login_register=(TextView)findViewById(R.id.tv_login_register); tv_login_register.setOnClickListener(this); tv=(TextView)findViewById(R.id.tv); } @Override public void onClick(View view) { String phone = et_phone.getText().toString(); String password=et_password.getText().toString(); System.out.println(phone + "," + password); System.out.println("按钮点击的线程的名字为:" + Thread.currentThread().getName()); Mythread mythread= new Mythread(phone,password); mythread.start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); tv.setText(Thread.currentThread().getName() + msg.obj); // 在把这个字符串变成一个JSONObejct try { JSONObject obj = new JSONObject(msg.obj.toString()); String value = obj.getString("loginstate"); System.out.println("value-->" + value); if (value.equals("success")) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "登录失败",Toast.LENGTH_SHORT ).show(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; private class Mythread extends Thread { private String phone; private String password; public Mythread(String phone, String password) { this.phone = phone; this.password = password; } @Override public void run () { // 在线程中请求服务器的资源 // Apache HttpClient // 1.创建HttpClient对象 HttpClient httpClient = new DefaultHttpClient(); Log.d("test", "t0"); // 2.构建请求的URL String url = "http://172.20.10.3:8080/appnews/user?phone=" + phone + "&password=" + password + "&operator=login"; // String url = "http://10.0.2.2:8080/appnews/servlet/StudentServlet"; // 3.创建HttpGet请求的对象 HttpGet httpGet = new HttpGet(url); Log.d("test4", "t4"); // 4.使用创建的httpClient执行这个httpGet请求 try { HttpResponse response = httpClient.execute(httpGet); Log.d("test3", "t3"); // 5.判断服务器端请求和应答是否成功 if (response.getStatusLine().getStatusCode() == 200) { System.out.println("请求和应答成功"); // 服务器端传输的数据的一个android封装的对象 HttpEntity entity = response.getEntity(); String msg = EntityUtils.toString(entity, "UTF-8"); System.out.println("server-->" + msg); // 在android中,子线程不能更新界面组件的值, // 因为界面组件是单线程模型,必须回到主线程更新组件的值。 // tv.setText(msg); // 构建消息对象 Message smsg = new Message(); smsg.obj = msg; handler.sendMessage(smsg); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block Log.d("test5", "t5"); e.printStackTrace(); } } } } 问答

Android Studio 应用无法访问本地服务器

08-07

代码在android studio 模拟器运行后本地服务器监听不到访问,可以确定后端代码没有问题,并且模拟器网络正常,可以使用模拟器里的浏览器访问服务器。 应用也添加了网络访问权限。 ``` <uses-permission android:name="android.permission.INTERNET" /> ``` 代码: ``` public class MainActivity extends AppCompatActivity { private TextView tvContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvContent = (TextView) findViewById(R.id.test1); // 这里页面上就一个简单的TextView,用于展示获取到报文内容 requestUsingHttpURLConnection(); } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what == 1){ tvContent.setText(msg.obj.toString()); } } }; private void requestUsingHttpURLConnection() { // 网络通信属于典型的耗时操作,开启新线程进行网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL("http://10.0.2.2:8080/ServletTest/RegisterServlet?account=123&password=abc"); // 声明一个URL,注意——如果用百度首页实验,请使用https //http://10.20.4.178:8080/ServletTest/RegisterServlet?account=123&password=abc Log.d("test", "开始连接"); connection = (HttpURLConnection) url.openConnection(); // 打开该URL连接 connection.setRequestMethod("GET"); // 设置请求方法,“POST或GET”,我们这里用GET,在说到POST的时候再用POST connection.setConnectTimeout(8000); // 设置连接建立的超时时间 connection.setReadTimeout(8000); // 设置网络报文收发超时时间 InputStream in = connection.getInputStream(); // 通过连接的输入流获取下发报文,然后就是Java的流处理 Log.d("test", "获取连接"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } Message msg = new Message(); msg.what = 1; msg.obj = response.toString(); Log.d("test", response.toString()); handler.sendMessage(msg); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } } ``` URL应该没有问题,我通过日志输出的URL可以在模拟器浏览器成功访问服务器 ``` public class Consatant { public static String URL = "http://10.0.2.2:8080/ServletTest/"; // IP地址请改为你自己的IP public static String URL_Register = URL + "RegisterServlet"; public static String URL_Login = URL + "LoginServlet"; } ``` 问答

Android作为Socket通信客户端的代码分析

07-14

基于Android Studio编写Socket的客户端,与基于Python的Socket服务器进行通信。 这是MainActivity相关代码: ``` import android.annotation.SuppressLint; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.view.View.OnClickListener; import android.os.Bundle; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; public class MainActivity extends AppCompatActivity { private TextView result = null; private TextView result2 = null; private static final String HOST = "192.168.80.100"; private static final int PORT = 12345; private Socket socket = null; private BufferedReader bufferedReader = null; private PrintWriter printWriter = null; private String content = ""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText eturl= findViewById(R.id.eturl); Button btn= findViewById(R.id.btn); result = findViewById(R.id.result); result2 = findViewById(R.id.result2); initSocket(); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String msg; msg = eturl.getText().toString(); if (socket.isConnected()){ if (!socket.isOutputShutdown()){ Toast.makeText(MainActivity.this,"正在发送,请稍等……",Toast.LENGTH_LONG).show(); sendMsg(msg); result.setText("正在连接" + HOST + "\n" + "发送URL" + "\n" + "等待接收检测结果" + "\n"); recv(); } } } }); } public void sendMsg(final String msg){ new Thread(new Runnable() { @Override public void run() { if(socket.isConnected()){ if(!socket.isOutputShutdown()){ printWriter.println(msg); } } } }).start(); } public void initSocket(){ new Thread(new Runnable() { @Override public void run() { try{ socket=new Socket(HOST,PORT); bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream())); printWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); }catch (Exception e){ e.printStackTrace(); } } }).start(); } public void recv(){ new Thread(new Runnable() { @Override public void run() { try{ while (true){ if(socket.isConnected()){ if(!socket.isInputShutdown()){ if((content = bufferedReader.readLine()) != null){ content += "\n"; mhandler.sendMessage(mhandler.obtainMessage()); }else{ } } } } }catch (Exception e){ e.printStackTrace(); } } }).start(); } @SuppressLint("HandlerLeak") public Handler mhandler = new Handler(){ public void handleMessage(Message msg) { super.handleMessage(msg); result2.setText(content); } }; } ``` 目前可以正常发送,但接收时有些问题,收不到,安卓客户端没有任何显示。服务器端用另外基于Python的客户端测试过,没问题,应该就是安卓客户端与接收有关的代码可能有问题。 从上面的代码中能看出有什么不妥吗,急求大神指点!!! 问答

安卓HttpURLConnection通过POST连接服务器问题

09-26

先上代码,我估计是最简单的一个发起通信的请求了吧(然而萌新真的很懵)。 ``` public void sendRequestWithHttp(){ new Thread(new Runnable() { HttpURLConnection httpURLConnection; JSONObject jsonObject=new JSONObject(); @Override public void run() { try{ jsonObject.put("CarId",1); URL url=new URL("http://192.168.1.108:8080/transportservice/type/jason/action/GetCarSpeed"); httpURLConnection=(HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setReadTimeout(8000); httpURLConnection.setUseCaches(false); DataOutputStream dataOutputStream=new DataOutputStream(httpURLConnection.getOutputStream()); String message=String.valueOf(jsonObject); dataOutputStream.writeBytes(message); dataOutputStream.flush(); dataOutputStream.close(); httpURLConnection.disconnect(); }catch (Exception e){ e.printStackTrace(); } } }).start(); } ``` emmmm大佬们,我现在只想向服务器发送数据,只要发过去就好QwQ 然后无论我怎么点击发送按钮(上方代码放在按钮事件中),服务器都没有一丝丝的反应(url地址是没问题的,这个可以放心)。 ``` Setting device_provisioned has moved from android.provider.Settings.Secure to android.provider.Settings.Global. jank_removeInvalidNode all the node in jank list is out of time ``` (尤其是第二个,报的最多,我百度了好久也没明白什么意思) (顺便问一下大佬,这个HttpURLConnection是从哪一步开始向服务器发起连接请求及数据的---比如OkHttp里面有一句 ``` Request request=new Request.Builder().url("http://192.168.1.108:8080/transportservice/type/jason/action/GetCarSpeed").post(requestBody).build(); ``` 起这个作用的。 ) 下面是修改后的代码,至于原因—大概就是不同的那一行出的问题吧 ``` JSONObject jsonObject=new JSONObject(); jsonObject.put("CarId",1); URL url=new URL("http://192.168.1.108:8080/transportservice/type/jason/action/GetCarSpeed"); httpURLConnection=(HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("Content-Type","application/json"); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setReadTimeout(500000); httpURLConnection.setConnectTimeout(8000); httpURLConnection.setUseCaches(false); httpURLConnection.getOutputStream().write(String.valueOf(jsonObject).getBytes()); int code=httpURLConnection.getResponseCode(); httpURLConnection.disconnect(); ``` 总而言之,只修改了两个地方: 一个是读数据的时间,一个是发送数据的方式。 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览