Tag: 获取

如何使用共享首选项保存数据?

我是android application.i中的新开发人员使用共享偏好概念将数据从一个活动共享到另一个activity.i已经实现了如下代码 Main.java public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TableLayout table=(TableLayout)findViewById(R.id.tableLayout1); table.removeAllViews(); String sName = null; for(int i=0;i”+sName); getSharedPreferences(“Values”, 0).edit().putString(“NAMES”,sName).commit(); name.setGravity(Gravity.LEFT); name.setTypeface(Typeface.MONOSPACE); row.addView(name); table.addView(row); } ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent it=new Intent(Main.this,Main2.class); startActivity(it); } }); } 在这里,我打印一个名字十次,并以共享偏好保存 TextView name=new TextView(Main.this); name.setText(” HAI […]

如何以编程方式添加自签名证书以从java代码发出HTTPS请求?

以下代码片段是从HTTP URL获取JSon响应: private static void getJson(String location) { try { try { createSSLSocket(); URL url = new URL( “https://abc.com/key/one”); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod(“GET”); conn.setRequestProperty(“Accept”, “application/json”); if (conn.getResponseCode() != 200) { throw new RuntimeException(“Failed : HTTP error code : ” + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; System.out.println(“Output […]

GET HTTP请求有效负载

我正在设计一个API,我想知道在GET请求上发送JSON有效负载是否合适? 在这个其他问题的HTTP请求方法的有效载荷 ,我们可以根据这个链接找到: 头部 – 没有定义的身体语义。 GET – 没有定义的正文语义。 PUT – 身体支持。 POST – 支持身体。 DELETE – 没有定义的正文语义。 TRACE – 身体不受支持。 选项 – 支持身体但没有语义(可能在将来)。 这是否意味着我不应该发送带有效负载的GET请求? 这样做有风险吗? 就像让一些HTTP客户端库无法发送这样的有效载荷一样? 或者我的Java API代码无法在某些应用程序服务器上移植? 还要别的吗? 我发现ElasticSearch在GET请求上使用了这样的有效负载: $ curl -XGET ‘http://localhost:9200/twitter/tweet/_search?routing=kimchy’ -d ‘{ “query”: { “filtered” : { “query” : { “query_string” : { “query” : “some query string here” } […]

在java中执行javascript – 打开URL并获取链接

import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.io.FileReader; public class Main { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName(“js”); try { FileReader reader = new FileReader(“C:/yourfile.js”); engine.put(“urlfromjava”, “http://www.something.com/?asvb”); engine.eval(reader); reader.close(); } catch (Exception e) { e.printStackTrace(); } } } 现在,yourfile.js包含这一行 function urlget(url) { print(“URL:”+url); var loc = window.open(url); var […]

常见的HTTP客户端和代理

我正在使用apache的常见httpclient库。 是否可以通过代理发出HTTP请求? 更具体地说,我需要使用代理列表来处理multithreadingPOST请求(现在我正在使用单线程GET请求进行测试)。 我试过用: httpclient.getHostConfiguration().setProxy(“67.177.104.230”, 58720); 我得到该代码的错误: 21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect 21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry INFO: Retrying request 21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect 21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry INFO: Retrying request 21.03.2012. 20:49:21 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry […]

如何使用Java在Http Get方法中设置Cookies

我想用cookies手动GET以下载和解析网页。 我需要提取安全令牌,以便在论坛上发帖。 我已完成登录,已阅读响应并提取了cookie(3对(名称,值))。 然后我写了包含这样的cookie的String: CookieString=”name1=value1; name2=value2; name3=value3″ 然后我做以下事情 HttpURLConnection connection connection = (HttpURLConnection)(new URL(Link).openConnection()); connection.setRequestMethod(“GET”); connection.setRequestProperty(“Connection”, “Keep-Alive”); connection.setRequestProperty(“Cookie”, CookieString ); connection.connect(); 然后我阅读了该页面,但它显示我没有登录论坛。 我究竟做错了什么? 编辑:我知道如果我想发帖,我必须提取安全令牌。 我的思路是,为了提取它,我需要获取这个特定的页面。 但是为了将安全令牌作为隐藏字段,我必须在线,因此我需要cookie。 但是,当我获取页面并设置上面提到的cookie时,我将该页面作为访客,它显示我不在线,安全令牌的值是guest,这对我没用。 我会检查你给我的链接,希望能找到解决方案。