我试图登录到网站,并保持该会话/ cookie的,这样服务器会识别出我的登录信息,但我在努力搞清楚提取响应中的cookie,并设置为保持我的登录请求的方式.我想知道我是否应该采用标题"Set-Cookie"或使用CookieStore.任何帮助是极大的赞赏.这是我的代码,其中包含我认为getHeader/getCookie方法的注释.
public class Http
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request;
HttpEntity entity;
HttpResponse response;
HttpPost post;
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
public static void setContext()
{
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
public static void getPage(String url) throws Exception
{
request = new HttpGet(url);
response = client.execute(request, localContext);
PARSER.preParse(url, response);
}
public static HttpResponse postPage(List<NameValuePair> params, String host, String action) throws Exception
{
post = new HttpPost(host + action);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
response = client.execute(post, …Run Code Online (Sandbox Code Playgroud) 使用Ecto 2.0:
defmodule PlexServer.BoardInstanceTest do
use PlexServer.ModelCase
alias PlexServer.BoardInstance
@valid_attrs %{board_pieces: [%PlexServer.BoardTileInstance{x: 0, y: 0}], empire: %PlexServer.EmpireInstance{}}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = BoardInstance.changeset(%BoardInstance{}, @valid_attrs)
assert changeset.valid?
end
end
defmodule PlexServer.BoardInstance do
use PlexServer.Web, :model
alias PlexServer.BoardTileInstance
schema "board_instances" do
belongs_to :empire, PlexServer.EmpireInstance
has_many :board_pieces, BoardTileInstance
timestamps
end
@required_fields ~w()
@optional_fields ~w()
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> cast_assoc(:board_pieces, required: true)
|> cast_assoc(:empire, require: true)
end
end
Run Code Online (Sandbox Code Playgroud)
我的测试失败了
**(RuntimeError)不支持使用cast/3转换关联,而是使用cast_assoc/3
看一下文档就说cast_assoc/3需要在cast/3之后调用,所以我很确定我错过了让这个测试工作必不可少的东西. …
** (Ecto.ConstraintError) constraint error when attempting to insert model:
* foreign_key: matches_person_id_fkey
Run Code Online (Sandbox Code Playgroud)
这是我的架构和迁移:
def change do
create table(:matches) do
add :percentage, :float
add :person_id, references(:persons)
add :star_id, references(:stars)
timestamps
end
create index(:matches, [:person_id])
end
schema "matches" do
field :percentage, :float
belongs_to :person, Person
belongs_to :star, Star
timestamps
end
Run Code Online (Sandbox Code Playgroud)
我在调用Repo.insert(changeset)时使用的变更集是:
%Ecto.Changeset{action: nil,
changes: %{percentage: 0.513657, person_id: 55, star_id: 1}, constraints: [],
errors: [], filters: %{},
model: %App.Match{__meta__: #ecto.Schema.Metadata<:built>, id: nil,
inserted_at: nil, percentage: nil,
person: #ecto.Association.NotLoaded<association :person is not loaded>,
person_id: …Run Code Online (Sandbox Code Playgroud) 我试图解析HTML的特定数据,但我遇到了返回字符的问题,至少我认为这就是问题所在.我正在使用一个简单的子字符串方法来拆分HTML,因为我事先知道我在寻找什么.
这是我的解析方法:
public static void parse(String response, String[] hashItem, String[][] startEnd) throws Exception
{
for (i = 0; i < hashItem.length; i++)
{
part = response.substring(response.indexOf(startEnd[i][0]) + startEnd[i][0].length());
value = part.substring(0, part.indexOf(startEnd[i][1]));
DATABASE.setHash(hashItem[i], value);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是给我提出问题的HTML示例
<table cellspacing=0 cellpadding=2 class=smallfont>
<tr onclick="lu();" onmouseover="style.cursor='hand'">
<td class=bodybox nowrap> 21,773,177,147 $ </td><td></td>
<td class=bodybox nowrap> 629,991,926 F </td><td></td>
<td class=bodybox nowrap> 24,537 P </td><td></td>
<td class=bodybox nowrap> 0 T </td>
<td></td><td class=bodybox nowrap> RT </td>
Run Code Online (Sandbox Code Playgroud)
有隐藏的返回字符,但是当我尝试将它们添加到我尝试使用的字符串中时,如果有的话,它不会很好.是否有一种方法或者更好的方法从HTML中删除隐藏的字符以使其更容易解析?一如既往地非常感谢任何帮助.
我的目标是将某些drawable的所有int ID存储在TypedArray中,以便我可以按顺序访问ID.例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array
name="bitmap_ids">
<item>@drawable/icon1</item>
<item>@drawable/icon2</item>
<item>@drawable/icon3</item>
<item>@drawable/icon4</item>
<item>@drawable/icon5</item>
</array>
</resources>
Run Code Online (Sandbox Code Playgroud)
这不会产生我正在寻找的int结果,以便我以后可以执行以下操作:
TypedArray typedArray = mResources.obtainTypedArray(R.array.bitmapIds);
int[][] t = new int[width][height];
int index = 0;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
t[i][j] = typedArray.getInt(index, -1);
index++;
}
Run Code Online (Sandbox Code Playgroud)
甚至后来:
return BitmapFactory.decodeResource(mResources, t[x][y]);
Run Code Online (Sandbox Code Playgroud)
这相当于:
return BitmapFactory.decodeResource(mResources, R.drawable.icon1);
Run Code Online (Sandbox Code Playgroud)
任何提示或见解将不胜感激.提前致谢.
TL; DR:我想传递一些代码,然后在try catch Pseudo代码中执行:
void ExecuteInTryCatch(BlockOfCode)
{
try
{
execute BlockOfCode with possible return value
}
catch (MyException e)
{
do something
}
}
Run Code Online (Sandbox Code Playgroud)
长版本:我正在编写Web应用程序的自动化测试,并希望执行一些代码,这些代码将捕获可能由非确定性原因导致的某些异常,并在最终抛出异常之前重试执行该代码一段时间.我已经看过可能为此目的使用委托,但我很困惑如何实现这一点,我从未使用过lambda表达式,这更令人困惑.
目标是能够将此代码重用于各种硒动作.对于Ruby而言,这是相当简单的,但在C#中,并非如此.
这似乎是一个非常容易的问题,但是我无法弄明白,也无法在其他任何地方找到解决方案.我正在连接一个%内的字符串,由于某种原因,它在%之后添加了数字25.有人知道这个简单问题的解决方案吗?
String buttonCheck = "%26" + DATABASE.getValue("buttonCheck") + "%26";
Run Code Online (Sandbox Code Playgroud)
出来吧
"%2526value%2526"
Run Code Online (Sandbox Code Playgroud)
编辑:很明显,问题实际上是在URL编码中,我将在问题中添加更多相关数据.
我正在开发一个Android应用程序,它从一个站点解析HTML,并允许用户通过Android UI与它进行交互.我在将%编码到表单的参数时遇到问题.
public class CLASS extends Activity
{
DefaultHttpClient client = new DefaultHttpClient;
String url = "http://www.url.com"
HttpRequestBase method = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add("buttoncheck", "%26" + DATABASE.getValue("buttonCheck") + "%26");
//DATABASE is simply a class that handles a HashMap
HttpPost methodPost = (HttpPost) method;
methodPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
execute(method);
}
Run Code Online (Sandbox Code Playgroud)
而不是发送表格的价值
buttoncheck=%26value%26
Run Code Online (Sandbox Code Playgroud)
我明白了
buttoncheck=%2526value%2526
Run Code Online (Sandbox Code Playgroud) 尝试在对话框中创建一个Spinner时,我得到一个NullPointerException似乎无法调试它,因为代码看起来很稳固.不知道其他人有什么想法.任何帮助是极大的赞赏.
protected Dialog onCreateDialog(int id)
{
Dialog dialog;
switch(id) {
case DIALOG_SEND_PM:
Spinner spinner = (Spinner)findViewById(R.id.pm_server);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.server_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
dialog = new Dialog(PM.this);
dialog.setContentView(R.layout.send_pm_dialog);
dialog.setTitle(R.string.send_pm);
pmMessage = (EditText) dialog.findViewById(R.id.send_pm_box);
Button sendPm = (Button) dialog.findViewById(R.id.send_pm_button);
sendPm.setOnClickListener(PM.this);
break;
default:
dialog = null;
}
Run Code Online (Sandbox Code Playgroud)
我在adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)得到异常; 我将上下文更改为MyClass.this,异常下移到下一行,这让我感到困惑.我想知道它是否是一个具有空值的适配器,但是我在调用所有内容的方式与之前相同,而不是在对话框中.
相关的XML数据:
<LinearLayout>
<TextView/>
<LinearLayout>
<TextView/>
<EditText/>
<TextView/>
<Spinner
android:id="@+id/pm_server"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:background="@drawable/yblueborder"
android:textColor="#ABABAB"/>
</LinearLayout>
<Button/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
编辑了其余的数据,因此不会占用太多空间.
我试图将当前活动的引用传递给对象,但似乎无法找到一个可以轻松完成此操作的方法.以前我能够通过当前上下文的"this"引用来做这个简单的传递.然后我将我的活动从实现OnClickListener改为创建OnClickListener对象,以便我可以将其作为参数传递.这是当我在单击按钮后尝试创建对象时遇到麻烦,这意味着我不能再使用"this"传递活动,因为"this"现在是OnClickListener的引用.
public class MyActivity extends Activity {
private Object mObject;
private OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
Object mObject = new Object(this);
}
}
}
public class Object {
private Activity mActivity;
public Object(Activity a) {
mActivity = a;
mActivity.setContentView(R.layout.layout);
}
}
Run Code Online (Sandbox Code Playgroud)
看起来像一个简单的修复,但我找不到答案......
任何帮助是极大的赞赏.