douzao2590 2014-02-23 08:30
浏览 126

如何以编程方式更改连接的端口号-Android

In WAMP server the port number was changed to 81 instead of 80 (to be able for another app to run). However, when I attempt to connect to server in which WAMP port number changed, to get data from data base, just my app stopped! I want to set the port-number for connection programmatically. I attempt to do such things which are:

  • change the proxy setting manually from phone setting itself (which succeeded with me). But, what I am looking for to be able to do that programmatically.

  • attempt to append Android Proxy Library. (which does not succeed with me) just many errors !

  • attempt to append the prot number with connection call statement, but also this does not work. The call statement is:

    try {
        result=bringData("http://"+IpAddress+"/android_connect/test.php",dbname,dbpass);
    
    } catch (Exception e) {    
        // TODO Auto-generated catch block
        e.printStackTrace();
    
    }
    

But when I append port number to IpAddress this does not work also, as for example:

    try {
        result=bringData("http://192.168.0.20:81/android_connect/test.php",dbname,dbpass);

    } catch (Exception e) {    
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

The code that connect to data-base:

    public String  bringData(String url, String jsonValue1, String jsonValue2) throws JSONException { 
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    JSONObject json = new JSONObject();

    try {
        json.put("dbname",jsonValue1);
        json.put("dbpass", jsonValue2);

         JSONArray postjson=new JSONArray();
         postjson.put(json);

         // Post the data:
         httppost.setHeader("json",json.toString());
         httppost.getParams().setParameter("jsonpost",postjson);

         // Execute HTTP Post Request
         System.out.print(json);
         HttpResponse response = httpclient.execute(httppost);

         // for JSON:
         if(response != null) {
                InputStream is = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                try {
                        int count=0;
                         while ((line = reader.readLine()) != null) {
                             result=line; 
                         }

                     } catch (IOException e) {
                         e.printStackTrace();
                     } finally {
                         try {
                             is.close();
                        } catch (IOException e) {
                             e.printStackTrace();
                        }
                   }
              }

        } catch (ClientProtocolException e) {
                 // TODO Auto-generated catch block
        } catch (IOException e) {
                 // TODO Auto-generated catch block
        }

        return result; //returned results from php code 

    } 

In sum, If there are any ideas or solutions or source code that can use to change the port number of connection for Android app. please?

Thanks In advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 fesafe材料库问题
    • ¥35 beats蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统