问题描述
您好我想创建为Asterisk的接口应用,配置设置已正确完成,因为我有双重检查它。
Hi I am trying to create Application for Asterisk Interface, The configuration setup has been done properly as I have double checked it.
看来我为什么不能从的Asterisk服务器访问配置文件的原因与connection..I需要使用的cookie用于连接到登录sessin的同一实例做。
It seems the reason why I can't access Config files from Asterisk server has to do with connection..I need to use cookie for connecting to the same instance of login sessin.
但到目前为止,我无法正确地使用Cookie的....需要一些帮助。
but so far I am unable to use Cookie Properly.... need little help.
我应该通过按钮1(BT1)放preSS登录;通过按钮2获取配置文件(BTN2)
I am supposed to login by press of button1 (bt1) & get config file by button2 (btn2)
公共类AsteriskInterface延伸活动{
public class AsteriskInterface extends Activity {
private static final String TAG = " AsteriskInterface";
EditText ed1;
Button bt1,bt2;
static String url ="http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
static String logincmd ="http://192.168.8.2:8088/asterisk/rawman?action=login&username=admin&secret=admin123";
static String getfilecmd = "http://192.168.8.2:8088/asterisk/rawman?action=getconfig&filename=sip.conf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) this.findViewById(R.id.editText);
bt1 = (Button) this.findViewById(R.id.submit);
bt2 = (Button) this.findViewById(R.id.config);
final WebView myWebView = (WebView) findViewById(R.id.webView);
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try{
new Execute().execute(logincmd);
} catch(Exception e) {
Log.v("Exception execute",
"Exception:"+e.getMessage());
}
}
});
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try{
new Execute().execute(getfilecmd);
} catch(Exception e) {
Log.v("Exception execute",
"Exception:"+e.getMessage());
}
//myWebView.loadUrl(cmd2);
}
});
}
public static void Response(String result) {
Log.e(TAG,result);
}
}
类执行扩展的AsyncTask {
class Execute extends AsyncTask {
List<String> cookies = null;
protected String doInBackground(String... searchKey) {
String cmd = searchKey[0];
try {
return action2(cmd);
} catch(Exception e) {
Log.v("Exception execute do back ground",
"Exception:"+e.getMessage());
return "";
}
}
protected void onPostExecute(String result) {
try {
AsteriskInterface.Response(result);
} catch(Exception e) {
Log.v("Exception excecute onPost Execute",
"Exception:"+e.getMessage());
}
}
private String action2(String uRL)
throws MalformedURLException, IOException {
StringBuilder response = new StringBuilder();
Log.v("Execute","execute url:"+uRL);
URL url = new URL(uRL);
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
DataOutputStream out;
DataInputStream in;
if(cookies==null){
httpconn.getHeaderField("Set-Cookie");
}
if(cookies!=null){
for(String cookie : cookies){
httpconn.setRequestProperty("Cookie", cookie);
}
}
httpconn.setDoOutput(true);
String post = "mobile_app="+URLEncoder.encode("1","UTF-8");
out = new DataOutputStream(httpconn.getOutputStream());
out.writeBytes(post);
out.flush();
out.close();
in = new DataInputStream(httpconn.getInputStream());
String line = "";
String data = "";
while((line=in.readLine())!=null){
data+=line;
}
if(httpconn.getResponseCode()==HttpURLConnection.HTTP_OK) {
BufferedReader input = new BufferedReader(
new InputStreamReader(httpconn.getInputStream()),
8192);
String strLine = null;
while ((strLine = input.readLine()) != null) {
response.append(strLine);
}
input.close();
}
return response.toString();
}
我可以做出来,我越来越连接到previous登录会话......但现在如何发出一个新的命令????
I can make out that I am getting connected to previous login session...but now how to issue a new command????
{
// 10月3日至28日:02:15.861:V /执行(4261):执行网址:
// 10月3日至28日:02:18.871:V /异常执行做回地面(4261):异常:已经连接
// 10月3日至28日:02:23.651:V /执行(4261):执行网址:
// 10月3日至28日:02:26.691:V /异常执行做回地面(4261):异常:已经连接
// 10月3日至28日:02:26.721:D / dalvikvm(4261):GC_CONCURRENT释放226K,8%的免费7430K / 8007K,暂停24ms + 3ms的,总61ms
}
{//03-28 10:02:15.861: V/Execute(4261): execute url:http://192.168.8.x:8088/asterisk/rawman?action=login&username=admin&secret=admin123//03-28 10:02:18.871: V/Exception execute do back ground(4261): Exception:Already connected//03-28 10:02:23.651: V/Execute(4261): execute url:http://192.168.8.x:8088/asterisk/rawman?action=getconfig&filename=sip.conf//03-28 10:02:26.691: V/Exception execute do back ground(4261): Exception:Already connected//03-28 10:02:26.721: D/dalvikvm(4261): GC_CONCURRENT freed 226K, 8% free 7430K/8007K, paused 24ms+3ms, total 61ms}
推荐答案
私人字符串操作(字符串URL)
抛出MalformedURLException的,IOException异常{
private String action(String uRL) throws MalformedURLException, IOException {
StringBuilder response = new StringBuilder();
Log.v("Execute","execute url:"+uRL);
URL url = new URL(uRL);
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
if(sCookie!=null && sCookie.length()>0){
httpconn.setRequestProperty("Cookie", sCookie);
}
if(httpconn.getResponseCode()==HttpURLConnection.HTTP_OK) {
BufferedReader input = new BufferedReader(
new InputStreamReader(httpconn.getInputStream()),
8192);
String strLine = null;
while ((strLine = input.readLine()) != null) {
response.append(strLine);
}
input.close();
}
String cookie = httpconn.getHeaderField("set-cookie");
if(cookie!=null && cookie.length()>0){
sCookie = cookie;
}
return response.toString();
}
这篇关于如何使用cookies HttpURLConnection类为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!